0

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

我有这样的线条,

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

以上行必须根据第二列进行排序,即澳大利亚、德国、印度。

所以,结果应该是,

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

数据来自文本文件

4

3 回答 3

2

我建议使用 aTreeSet并阅读您的文本文件,并将您的数据保存在实现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());
        }
    }
}
于 2012-08-26T08:31:26.550 回答
1

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

Row Class表示行数据

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


}
于 2012-08-26T09:05:24.507 回答
0

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

使用

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

将按自然顺序对您的密钥集进行排序。然后,您可以遍历键集并以排序方式访问哈希图中的每一行。

于 2012-08-26T08:07:45.800 回答