给容器内的类对象排序
核心是Collections.sort(容器,【自定义比较器】)
自定义比较函数返回值为int类型,类对象本身为第一个比较对象o1,参数对象为第二个比较对象o2
若返回值大于0,则o1排在o2后面;若返回值小于0,则o2排在o1后面;若返回值等于0,则按容器内原顺序排列
方法一:所比较对象的所属类实现Comparable接口,并重写compareTo方法 使用Collections.sort(容器)
public class App
{
public static void main( String[] args )
{
List<Cat> cats=new ArrayList<Cat>();
Cat cat1=new Cat("Tom",24,13.5);
Cat cat2=new Cat("Lucy",23,13.5);
Cat cat3=new Cat("Tom",23,33.5);
Cat cat4=new Cat("Jack",23,23.5);
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
cats.add(cat4);
Collections.sort(cats);
Iterator<Cat> it=cats.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Cat implements Comparable
{
//姓名
public String name;
//年龄
public int age;
//体重
public double weight;
public Cat(String name, int age, double weight) {
this.name=name;
this.age=age;
this.weight=weight;
}
@Override
public int compareTo(Object o) //按姓名排序
{
Cat c2=(Cat) o;
return name.compareTo(c2.name);
}
@Override
public int compareTo(Object o) //按年龄排序
{
Cat c2=(Cat) o;
return age-c2.age;
}
@Override
public int compareTo(Object o) //按姓名和年龄排序
{
Cat c2=(Cat) o;
if(name.compareTo(c2.name)!=0)
return name.compareTo(c2.name);
else
return age-c2.age;
}
public String toString()
{
return "name:"+name+",age:"+age+",weight:"+weight;
}
}
方法二:自定义比较器类实现Comparator接口,重写compare方法 使用Collections.sort(容器,自定义比较器类对象)
public class App
{
public static Comparator<Cat> c=new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Cat c1=(Cat) o1;
Cat c2=(Cat) o2;
return c1.name.compareTo(c2.name);
}
@Override
public int compare(Object o1, Object o2) {
Cat c1=(Cat) o1;
Cat c2=(Cat) o2;
return c1.age-c2.age;
}
@Override
public int compare(Object o1, Object o2) {
Cat c1=(Cat) o1;
Cat c2=(Cat) o2;
if(c1.name.compareTo(c2.name)!=0)
return c1.name.compareTo(c2.name);
return c1.age-c2.age;
}
};
public static void main( String[] args )
{
List<Cat> cats=new ArrayList<Cat>();
Cat cat1=new Cat("Tom",24,13.5);
Cat cat2=new Cat("Lucy",23,13.5);
Cat cat3=new Cat("Tom",23,33.5);
Cat cat4=new Cat("Jack",23,23.5);
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
cats.add(cat4);
Collections.sort(cats,c);
Iterator<Cat> it=cats.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Cat
{
//姓名
public String name;
//年龄
public int age;
//体重
public double weight;
public Cat(String name, int age, double weight) {
this.name=name;
this.age=age;
this.weight=weight;
}
public String toString()
{
return "name:"+name+",age:"+age+",weight:"+weight;
}
}
class Com implements Comparator
{
@Override
public int compare(Object o1, Object o2) {
Cat c1=(Cat) o1;
Cat c2=(Cat) o2;
return c1.name.compareTo(c2.name);
}
}
public class App
{
public static void main( String[] args )
{
List<Cat> cats=new ArrayList<Cat>();
Cat cat1=new Cat("Tom",24,13.5);
Cat cat2=new Cat("Lucy",23,13.5);
Cat cat3=new Cat("Tom",23,33.5);
Cat cat4=new Cat("Jack",23,23.5);
cats.add(cat1);
cats.add(cat2);
cats.add(cat3);
cats.add(cat4);
Com c=new Com();
Collections.sort(cats,c);
Iterator<Cat> it=cats.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
class Cat
{
//姓名
public String name;
//年龄
public int age;
//体重
public double weight;
public Cat(String name, int age, double weight) {
this.name=name;
this.age=age;
this.weight=weight;
}
public String toString()
{
return "name:"+name+",age:"+age+",weight:"+weight;
}
}