List集合对象根据字段排序
2014-04-07 23:57
281 查看
//把需要比较的对象实现Comparable接口实现compareTo方法
public class Address implements Comparable<Address> {
String country;
String city;
String name;
public Address(String country, String city, String name) {
super();
this.country = country;
this.city = city;
this.name = name;
}
public String toString(){
return "\nname:"+this.name+" city:"+this.city+" country:"+this.country;
}
@Override
public int compareTo(Address o) {
//如果国家不相等,那么直接比较其他字段
if(!this.country.equals(o.country)){
return this.country.compareTo(o.country);
}else if(!this.city.equals(o.city)){
return this.city.compareTo(o.city);
}else{
return this.name.compareTo(o.name);
}
}
}
//测试类
public class ComparableTest {
public static void main(String[] args) {
List<Address> list = new ArrayList<Address>();
Address a1 = new Address("中国", "湖南", "屌丝1");
Address a2 = new Address("中国", "湖北", "屌丝2");
Address a3 = new Address("美国", "纽约", "屌丝3");
Address a4 = new Address("中国", "湖北", "屌丝4");
Address a5 = new Address("中国", "湖南", "屌丝5");
Address a6 = new Address("中国", "广西", "屌丝6");
list.add(a1);
list.add(a2);
list.add(a3);
list.add(a4);
list.add(a5);
list.add(a6);
System.out.println(list);//排序前
Collections.sort(list);
System.out.println(list);//排序后
}
}
}
//打印结果
[ name:屌丝1 ncity:湖南 ncountry:中国,
name:屌丝2 ncity:湖北 ncountry:中国,
name:屌丝3 ncity:纽约 ncountry:美国,
name:屌丝4 ncity:湖北 ncountry:中国,
name:屌丝5 ncity:湖南 ncountry:中国,
name:屌丝6 ncity:广西 ncountry:中国]
[ name:屌丝6 ncity:广西 ncountry:中国,
name:屌丝2 ncity:湖北 ncountry:中国,
name:屌丝4 ncity:湖北 ncountry:中国,
name:屌丝1 ncity:湖南 ncountry:中国,
name:屌丝5 ncity:湖南 ncountry:中国,
name:屌丝3 ncity:纽约 ncountry:美国]
public class Address implements Comparable<Address> {
String country;
String city;
String name;
public Address(String country, String city, String name) {
super();
this.country = country;
this.city = city;
this.name = name;
}
public String toString(){
return "\nname:"+this.name+" city:"+this.city+" country:"+this.country;
}
@Override
public int compareTo(Address o) {
//如果国家不相等,那么直接比较其他字段
if(!this.country.equals(o.country)){
return this.country.compareTo(o.country);
}else if(!this.city.equals(o.city)){
return this.city.compareTo(o.city);
}else{
return this.name.compareTo(o.name);
}
}
}
//测试类
public class ComparableTest {
public static void main(String[] args) {
List<Address> list = new ArrayList<Address>();
Address a1 = new Address("中国", "湖南", "屌丝1");
Address a2 = new Address("中国", "湖北", "屌丝2");
Address a3 = new Address("美国", "纽约", "屌丝3");
Address a4 = new Address("中国", "湖北", "屌丝4");
Address a5 = new Address("中国", "湖南", "屌丝5");
Address a6 = new Address("中国", "广西", "屌丝6");
list.add(a1);
list.add(a2);
list.add(a3);
list.add(a4);
list.add(a5);
list.add(a6);
System.out.println(list);//排序前
Collections.sort(list);
System.out.println(list);//排序后
}
}
}
//打印结果
[ name:屌丝1 ncity:湖南 ncountry:中国,
name:屌丝2 ncity:湖北 ncountry:中国,
name:屌丝3 ncity:纽约 ncountry:美国,
name:屌丝4 ncity:湖北 ncountry:中国,
name:屌丝5 ncity:湖南 ncountry:中国,
name:屌丝6 ncity:广西 ncountry:中国]
[ name:屌丝6 ncity:广西 ncountry:中国,
name:屌丝2 ncity:湖北 ncountry:中国,
name:屌丝4 ncity:湖北 ncountry:中国,
name:屌丝1 ncity:湖南 ncountry:中国,
name:屌丝5 ncity:湖南 ncountry:中国,
name:屌丝3 ncity:纽约 ncountry:美国]
相关文章推荐
- List集合根据存储对象的属性字段排序实现
- J2SE基础夯实系列之List中的数据如何根据对象的某一个或多个字段排序引出Comparable和comparator的使用
- List集合中的对象根据属性排序
- Java面试题,List集合中根据对象的某一属性排序
- 根据对象的某个字段对List<Object>排序
- C#中对List<Object>泛型集合,根据对象的某一属性进行升序排序
- List集合中的对象根据属性排序
- list集合根据对象属性排序举例
- List集合中的对象根据属性排序
- 根据对象中的重复字段,取出list集合中的重复对象
- List集合中的对象根据属性排序
- list集合中根据对象排序的问题
- List集合中的对象根据属性排序
- 如何根据对象的属性,对集合(list / set)中的对象进行排序
- J2SE基础夯实系列之List中的数据如何根据对象的某一个或多个字段排序引出Comparable和comparator的使用-- Collections与Arrays
- List<T>泛型集合根据对象的某一属性排序
- 容易遗忘的Comparable:一个结果集List,里面有很多的对象,要求根据对象的指定(age)字段进行排序
- 根据对象的某个字段对List<Object>排序
- List集合中的对象根据属性排序
- List集合中的对象根据属性排序