2012-08-26 51 views
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 

数据是从文本文件

+0

这将是微不足道的提供用户能够做到这一点,使用'JTable'。虽然我想这不是你想要实现的功能。 ;) –

+0

它们是列表或表记录的对象吗?当你正在写或记录你的数据,你可以使用冒泡排序算法来排序 –

+0

这是一个文本文件 – FirmView

回答

0

我建议你阅读行文件中的行和使用的StringTokenizer每行有机会获得单话。然后你可以把每一行放到一个以国家为关键字的HashMap中。

使用

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

能够时不时的关键自然顺序设置。然后,您可以遍历密钥集并以排序的方式访问hashmap中的每一行。

+0

这是一个文本文件 – FirmView

+0

好吧,那么我建议你逐行阅读该文件,并在每一行上使用StringTokenizer可以访问单个单词。然后,您可以将该结构放入与国家相关的集合中,并调用Collection.sort(yourCollection);它将以非天然的顺序对收集品进行分类 – Markus

+0

@Markus:编辑您的答案并将其作为您答案的一部分。答案的方式,它看起来更像是一个“评论”而不是一个真正的“答案”:) – Sujay

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

可以结构数据,因为这模型

行类代表行数据

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


}