在java中经常会使用到排序,通常使用的java内置提供的排序方式:
- Arrays.sort()
- Collection.sort()
- Collections.sort()
- Stream中使用sort
对一批对象进行排序的方式:(ps:对对象的多个属性进行排序则多次使用排序方法即可,比如先对Person的age排序,再对name排序,则需要使用两次排序)
/**
* java排序方法:
* 1. Arrays.sort()
* 2. Collection.sort()
* 3. Collections.sort()
* 4. Stream中使用sort
*/
public class Sort {
public static void main(String[] args) {
List<Person> list = new ArrayList<>();
Person[] people = new Person[10];
for (int i = 0; i < 10; i++) {
Person person = new Person(i / 2, "l" + i);
people[i] = person;
list.add(person);
}
// Arrays.sort()
System.out.println("-------begin Arrays.sort()");
Arrays.sort(people, (o1, o2) -> {
if (o1.age > o2.age) {
return -1;
} else if (o1.age < o2.age) {
return 1;
} else {
return 0;
}
});
for (Person person : people) {
System.out.println(person);
}
System.out.println("------");
for (Person person : list) {
System.out.println(person);
}
System.out.println("-----");
list.sort((o1, o2) -> {
if (o1.age > o2.age) {
return -1;
} else if (o1.age < o2.age) {
return 1;
} else {
return 0;
}
});
for (Person person : list) {
System.out.println(person);
}
System.out.println("-----");
Collections.sort(list, (o1, o2) -> {
if (o1.age > o2.age) {
return 1;
} else if (o1.age < o2.age) {
return -1;
} else {
return 0;
}
});
for (Person person : list) {
System.out.println(person);
}
System.out.println("-----");
list.stream().sorted(Comparator.comparing(Person::getAge).reversed()).forEach(System.out::println);
}
}
class Person {
int age;
String name;
public Person() {
}
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
打印结果:
-------begin Arrays.sort()
Person{age=4, name='l8'}
Person{age=4, name='l9'}
Person{age=3, name='l6'}
Person{age=3, name='l7'}
Person{age=2, name='l4'}
Person{age=2, name='l5'}
Person{age=1, name='l2'}
Person{age=1, name='l3'}
Person{age=0, name='l0'}
Person{age=0, name='l1'}
------
Person{age=0, name='l0'}
Person{age=0, name='l1'}
Person{age=1, name='l2'}
Person{age=1, name='l3'}
Person{age=2, name='l4'}
Person{age=2, name='l5'}
Person{age=3, name='l6'}
Person{age=3, name='l7'}
Person{age=4, name='l8'}
Person{age=4, name='l9'}
-----
Person{age=4, name='l8'}
Person{age=4, name='l9'}
Person{age=3, name='l6'}
Person{age=3, name='l7'}
Person{age=2, name='l4'}
Person{age=2, name='l5'}
Person{age=1, name='l2'}
Person{age=1, name='l3'}
Person{age=0, name='l0'}
Person{age=0, name='l1'}
-----
Person{age=0, name='l0'}
Person{age=0, name='l1'}
Person{age=1, name='l2'}
Person{age=1, name='l3'}
Person{age=2, name='l4'}
Person{age=2, name='l5'}
Person{age=3, name='l6'}
Person{age=3, name='l7'}
Person{age=4, name='l8'}
Person{age=4, name='l9'}
-----
Person{age=4, name='l8'}
Person{age=4, name='l9'}
Person{age=3, name='l6'}
Person{age=3, name='l7'}
Person{age=2, name='l4'}
Person{age=2, name='l5'}
Person{age=1, name='l2'}
Person{age=1, name='l3'}
Person{age=0, name='l0'}
Person{age=0, name='l1'}