鄙人大概想了三种方式:
1.第一种:借助工具类 Collections
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestSet {
public static void main(String[] ars){
Person p1 = new Person("a", 20);
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("b", 20));
persons.add(new Person("a", 20));
persons.add(new Person("c", 21));
persons.add(new Person("d", 19));
persons.add(new Person("e", 19));
persons.add(new Person("f", 22));
persons.add(new Person("g", 23));
persons.add(new Person("h", 18));
persons.add(new Person("i", 24));
Collections.sort(persons,new Comparator<Person>(){
@Override
public int compare(Person o1, Person o2) {
return o1.getAge()==o2.getAge() ? o2.getName().compareTo(o1.getName()):o2.age-o1.age;
}
});
for (Person p :persons) {
System.out.println(p.getAge()+":"+p.getName());
}
}
}
class Person{
String name ;
int age ;
public Person(String name ,int age){
this.name = name;
this.age = age ;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
}
2:将List 转换数组排序 然后在转换List
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class TestSet {
public static void main(String[] ars){
Person p1 = new Person("a", 20);
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("b", 20));
persons.add(new Person("a", 20));
persons.add(new Person("c", 21));
persons.add(new Person("d", 19));
persons.add(new Person("e", 19));
persons.add(new Person("f", 22));
persons.add(new Person("g", 23));
persons.add(new Person("h", 18));
persons.add(new Person("i", 24));
Person[] pary = new Person[persons.size()];
pary =persons.toArray(pary);
Arrays.sort(pary, new Comparator<Person>(){
@Override
public int compare(Person o1, Person o2) {
return o1.getAge()==o2.getAge() ? o2.getName().compareTo(o1.getName()):o2.age-o1.age;
}
});
persons = Arrays.asList(pary);
for (Person p :persons) {
System.out.println(p.getAge()+":"+p.getName());
}
}
}
class Person{
String name ;
int age ;
public Person(String name ,int age){
this.name = name;
this.age = age ;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
}
3.利用ListIterator :
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class TestSet {
public static void main(String[] ars){
Person p1 = new Person("a", 20);
List<Person> persons = new ArrayList<Person>();
persons.add(new Person("b", 20));
persons.add(new Person("a", 20));
persons.add(new Person("c", 21));
persons.add(new Person("d", 19));
persons.add(new Person("e", 19));
persons.add(new Person("f", 22));
persons.add(new Person("g", 23));
persons.add(new Person("h", 18));
persons.add(new Person("i", 24));
//方法三
Object[] a = persons.toArray();
Arrays.sort(a,new Comparator<Object>(){
@Override
public int compare(Object o1, Object o2) {
Person p1 = (Person)o1;
Person p2 = (Person)o2;
return p1.getAge()==p2.getAge() ? p2.getName().compareTo(p1.getName()):p2.age-p1.age;
}
});
java.util.ListIterator<Person> li = persons.listIterator();
for (int i = 0; i < a.length; i++) {
li.next();
li.set((Person) a[i]);
}
for (Person p :persons) {
System.out.println(p.getAge()+":"+p.getName());
}
}
}
class Person{
String name ;
int age ;
public Person(String name ,int age){
this.name = name;
this.age = age ;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
}
此处,不用多说,如果排序规则不同,修改Comparator的比较逻辑即可