根据列对行进行排序

有什么方法可以根据列进行排序吗?

我有像,

1000 Australia Kangaroo Canberra 1002 India Tiger Delhi 1092 Germany Eagle Berlin 

上述行必须根据第二列进行分类,即澳大利亚,德国,印度。

所以,结果应该是,

 1000 Australia Kangaroo Canberra 1092 Germany Eagle Berlin 1002 India Tiger Delhi 

数据来自文本文件

我建议使用TreeSet并读取文本文件并将数据保存在实现Comparable的类中。 这样,当您添加到TreeSet ,数据将按排序顺序添加。

此示例可能有所帮助:

 class Data implements Comparable{ private int digits; private String country; private String animal; private String capital; public Data(int digits, String country, String animal, String capital){ this.digits = digits; this.country = country; this.animal = animal; this.capital = capital; } public int getDigits() { return digits; } public void setDigits(int digits) { this.digits = digits; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getAnimal() { return animal; } public void setAnimal(String animal) { this.animal = animal; } public String getCapital() { return capital; } public void setCapital(String capital) { this.capital = capital; } @Override public int compareTo(Data data) { return getCountry().compareTo(data.getCountry()); } } class TestCountry{ public static void main(String[] args) { TreeSet set = new TreeSet(); /** * Assuming that you can read the CSV file and build up the Data objects. * You would then put them in the set where they will be added in a sorted * fashion */ set.add(new Data(1000, "Australia", "Kangaroo", "Canberra")); set.add(new Data(1002, "India", "Tiger", "Delhi")); set.add(new Data(1092, "Germany", "Eagle", "Berlin")); for(Data data: set){ System.out.println(data.getDigits()+"\t"+data.getCountry()+"\t"+data.getAnimal()+"\t"+data.getCapital()); } } } 

您可以将数据构建为此模型

行类表示行数据

 public class Row implements Comparable { private int number; private String country; private String animal; private String city; public Row(int number, String country, String animal, String city) { super(); this.number = number; this.country = country; this.animal = animal; this.city = city; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getAnimal() { return animal; } public void setAnimal(String animal) { this.animal = animal; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } // Easy to print and show the row data @Override public String toString() { return "Row [number=" + number + ", country=" + country + ", animal=" + animal + ", city=" + city + "]"; } // sort based on column "country" @Override public int compareTo(Row o) { return this.country.compareTo(o.country); } 

}

并且测试示例将为

 public static void main(String[] args) { ArrayList data = new ArrayList(); data.add(new Row(1000, "Australia", "Kangaroo", "Canberra")); data.add(new Row(1002, "India", "Tiger", "Delhi")); data.add(new Row(1092, "Germany", "Eagle", "Berlin")); // To sort the data (based on column "country") Collections.sort(data); // Print and show the data for (int i = 0; i < data.size(); i++) { System.out.println(data.get(i)); } } 

我建议您逐行读取该文件,并在每一行上使用StringTokenizer来访问单个单词。 然后,您可以将每行放入HashMap,并将国家/地区作为键。

运用

 Collection k = yourHashMap.keySet(); Collections.sort(k); 

将按自然顺序排序您的键集。 然后,您可以迭代键集并以排序方式访问hashmap中的每一行。