Sorting lines based on column

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

Is there any way where we can sort based on columns?

I have lines like,

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

The above lines has to be sorted based on the second column, that is Australia, Germany, India.

So, the result should be,

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

Data is from text file

回答1:

I would suggest using a TreeSet and reading your text file and keeping your data in a class that implements Comparable. That way, when you're adding to the TreeSet the data would get added in a sorted order.

This example might help:

class Data implements Comparable<Data>{      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<Data> set = new TreeSet<Data>();         /**           * 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());         }     } } 


回答2:

You can struct your data as this model

Row Class represent row data

public class Row implements Comparable<Row> {  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); } 

}

and the test example will be as

public static void main(String[] args) {      ArrayList<Row> data = new ArrayList<Row>();     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));     }   } 


回答3:

I would suggest you read that file line by line and use a StringTokenizer on each line to have access to the single words. You can then put each line into a HashMap with the country as key.

Using

Collection<String> k = yourHashMap.keySet(); Collections.sort(k); 

Would sort your key set in natural order. You can then iterate over the key set and access each line in the hashmap in a sorted way.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!