1.创建对象的时候让对象具有可比较的性质,例如让Person实现Comparable接口
示例代码:让Person通过继承Comparable接口使其具有可比较属性—本例中先根据名称字符串从小到大比较,如果名字字符串相同再根据年龄从小到大比较。
class HelloWorld
{
public static void main(String[] args)
{
TreeSet<Person> ts = new TreeSet<Person>();
ts.add(new Person("Lili",11));
ts.add(new Person("xiaoming",14));
ts.add(new Person("zhang",13));
ts.add(new Person("xiaohong",12));
ts.add(new Person("xiaoming",11));
Iterator<Person> it = ts.iterator();
while(it.hasNext())
{
Person p = it.next();
System.out.println(p.name +"....."+p.age);
}
}
}
class Person implements Comparable<Person>
{
String name;
int age;
Person(String name,int age)
{
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person p) {
int num = name.compareTo(p.name);
if(num==0)
{
return this.age - p.age;
}
return num;
}
}
2.通过实现Comparator接口创建比较器对象,比较器对象可以给TreeSet构造方法使用,也可以在对List集合排序的时候Collections.sort()方法使用
TreeSet的构造方法:
TreeSet(Comparator<? super E> comparator)
构造一个新的空 TreeSet,它根据指定比较器进行排序。
对List集合排序Collecions.sort()方法:
sort(List<T> list, Comparator<? super T> c)
根据指定比较器产生的顺序对指定列表进行排序。
1)给TreeSet使用示例代码:
class HelloWorld
{
public static void main(String[] args)
{
TreeSet<Person> ts = new TreeSet<Person>(new Mycompare());
ts.add(new Person("Lili",11));
ts.add(new Person("xiaoming",11));
ts.add(new Person("xiaoming",14));
ts.add(new Person("zhang",13));
ts.add(new Person("xiaohong",12));
Iterator<Person> it = ts.iterator();
while(it.hasNext())
{
Person p = it.next();
System.out.println(p.name+"......"+p.age);
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
class Person //实现了Comparable接口
{
String name;
int age;
Person(String name,int age)
{
this.name = name;
this.age = age;
}
}
class Mycompare implements Comparator<Object>
{
public int compare(Object obj1,Object obj2)
{
Person p1 = (Person)obj1;
Person p2 = (Person)obj2;
//System.out.println(p1.name+p1.age+"compare..."+p2.name+p2.age);
int num = p1.name.compareTo(p2.name);
if(num==0)
{
return p1.age - p2.age;
}
return num;
}
}
2)给ArrayList链表排序示例代码:
class Person
{
String name;
int age;
Person(String name,int age)
{
this.name = name;
this.age = age;
}
}
class Mycompare implements Comparator<Object>
{
public int compare(Object obj1,Object obj2)
{
Person p1 = (Person)obj1;
Person p2 = (Person)obj2;
int num = p1.name.compareTo(p2.name);
if(num==0)
{
return p1.age - p2.age;
}
return num;
}
}
class HelloWorld
{
public static void main(String[] args)
{
ArrayList<Person> al = new ArrayList<>();
al.add(new Person("Lili",11));
al.add(new Person("xiaoming",11));
al.add(new Person("xiaoming",14));
al.add(new Person("zhang",13));
al.add(new Person("xiaohong",12));
Collections.sort(al,new Mycompare()); //对集合中的对象进行排序
for(Person p:al)
{
System.out.println(p.name+"....."+p.age);
}
}
}
3.对象构成的数组的排序
class TanXin{
public static void main(String[] args){
Integer[] g = {5,10,2,9,15,9}; //Integer类型对象的数组
Arrays.sort(g,new myComparator()); //对对象的数组排序
}
}
class myComparator implements Comparator<Integer>{
public int compare(Integer a,Integer b){
return (b-a);
}
}
说明:这里
g数组中必须是对象,不能是基本类型,例如int[] g = {5,10,2,9,15,9};
就不能使用
方法进行排序。
sort(T[] a, Comparator<? super T> c)
方法进行排序。