您可以将数据构建为此模型
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));
}
}