根据列对行进行排序

时间:2012-08-26 08:04:49

标签: java sorting

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

我有像

这样的行
1000 Australia     Kangaroo            Canberra
1002 India         Tiger               Delhi
1092 Germany       Eagle               Berlin

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

所以,结果应该是,

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

数据来自文本文件

3 个答案:

答案 0 :(得分:2)

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

此示例可能有所帮助:

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());
        }
    }
}

答案 1 :(得分:1)

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

行类表示行数据

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);
}

}

,测试示例将为

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));
    }


}

答案 2 :(得分:0)

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

使用

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

按自然顺序对键集进行排序。然后,您可以迭代密钥集并以排序的方式访问散列映射中的每一行。