在日常寫程序時,或做筆試題時總會遇到自己定義的類或者數據結構,如果把自己定義的類存入List這樣的數組中,排序是很麻煩的,比如:
下面定義了一個Person類
class Person{
String name;
int age;
public Person(String name,int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
如果我們要對存了很多Person的數組排序的話,首先要確定排序規定,就是按什么排序,比如按照年齡大小,按照名字之類的,如果我們采用常規方法取值在排序然后交換位置時很麻煩的,好在List為我們提供了sort方法,下面是jdk文檔
Parameters:
c - the Comparator used to compare list elements. A null value indicates that the elements’ natural ordering should be used
依舊是List的sort方法根據特定的Comparator排序。使用list.sort(c )的形式排序,c就是這個特定的Comparator對象。Comparator就是我們比較的准則。下面看啊可能Comparator類:
@FunctionalInterface
public interface Comparator< T>
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don’t have a natural ordering.
這是一個函數式接口,用於比較兩個對象,因為是函數式接口,所以我們可以采用Lambda表達式去實現這個接口,如果不知道什么是函數式接口或者有興趣用Lambda表達式,參考下面這篇博客:Lambda表達式
下面介紹一下如何實現這個接口:
上面是實現接口必須實現的方法,所以我們寫一個類實現這個接口,並且實現這個方法:
class PersonSortByAge implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return o1.age-o2.age;
}
}
這個就很好理解
- 如果返回值等於零:o1=o2
- 返回值大於零則o1>o2
- 返回值小於於零則o1<o2
然后就可以在集合中排序了:
public class ComparatorTest {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("ace",22));
personList.add(new Person("xb",21));
personList.add(new Person("glm",36));
personList.add(new Person("sxy",20));
personList.sort(new PersonSortByAge());
for(Person p:personList)
System.out.println(p);
}
}
結果如下,按照年齡大小排好了。
如果想試試別的排序方法,下面我給出例子:
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* 項目名: CSDN
* 包名: Compare
* 文件名: ComparatorTest.java
* 創建時間: 2019年4月13日
*
* @author: xiatom
* 描述: 比較方法——comparator
*
*
**/
class PersonSortByAgeSmall implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return o1.age-o2.age;
}
}
class PersonSortByAgeBig implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return o2.age-o1.age;
}
}
class PersonSortByName implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}
}
class Person{
String name;
int age;
public Person(String name,int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
public class ComparatorTest {
public static void main(String[] args) {
List<Person> personList = new ArrayList<Person>();
personList.add(new Person("ace",22));
personList.add(new Person("xb",21));
personList.add(new Person("glm",36));
personList.add(new Person("sxy",20));
System.out.println("按照年齡,大的排前面");
personList.sort(new PersonSortByAgeBig());
for(Person p:personList)
System.out.println(p);
System.out.println();
System.out.println("按照年齡,小的排前面");
personList.sort(new PersonSortByAgeSmall());
for(Person p:personList)
System.out.println(p);
System.out.println();
System.out.println("按照名字排序");
personList.sort(new PersonSortByName());
for(Person p:personList)
System.out.println(p);
}
}