如何按降序对Java ArrayList进行排序
通过使用Collections.reverseOrder(Comparator
<t>
cmp)方法,我们可以按相反的顺序对集合进行排序。 reverseOrder()方法根据给定的Comparator进行反转。如果为null,它将以自然顺序逆向收集。
让我们看一个简单的示例,以降序对ArrayList进行排序。
SmartPhone.java
import java.util.Comparator;
public class SmartPhone {
String brand;
String model;
intprice;
intrating;
SmartPhone(String brand,String model,intprice, intrating){
this.brand = brand;
this.model = model;
this.price = price;
this.rating = rating;
}
public String getBrand() {
returnbrand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
returnmodel;
}
public void setModel(String model) {
this.model = model;
}
public int getPrice() {
returnprice;
}
public void setPrice(intprice) {
this.price = price;
}
public int getRating() {
returnrating;
}
public void setRating(intrating) {
this.rating = rating;
}
public String toString() {
return"SmartPhone [brand=" + brand + ", model=" + model + ", price=" + price + ", rating=" + rating + "]";
}
public int compareTo(SmartPhone sp) {
returnthis.price - sp.price;
}
}
class RatingComparator implements Comparator<
SmartPhone>
{
@Override public int compare(SmartPhone obj1, SmartPhone obj2) {
return (obj1.rating<obj2.rating) ? -1 : (obj1.rating>obj2.rating) ? 1 : 0;
}
}
ArrayListLearning.java
public class ArrayListLearning {
@SuppressWarnings("unchecked") public static void main(String[] args) {
List<SmartPhone> phoneList = new ArrayList<>();
SmartPhone ph1 = new SmartPhone("Apple", "6s", 50000, 10);
SmartPhone ph2 = new SmartPhone("lg", "pro2", 40000, 9);
SmartPhone ph3 = new SmartPhone("MI", "3s", 10000, 6);
SmartPhone ph4 = new SmartPhone("Letv", "le2", 12000, 7);
phoneList.add(ph1);
phoneList.add(ph2);
phoneList.add(ph3);
phoneList.add(ph4);
System.out.println("Actual List");
System.out.println(phoneList);
System.out.println("Sorting the list as comparator");
Collections.sort(phoneList, new RatingComparator());
System.out.println(phoneList);
System.out.println("Reversing the Comparator sorting");
Comparator<SmartPhone> cmp = Collections.reverseOrder(new RatingComparator());
Collections.sort(phoneList, cmp);
System.out.println("Printing the reverse list");
System.out.println(phoneList);
}
}
输出:
Actual List
[SmartPhone [brand=Apple, model=6s, price=50000, rating=10],
SmartPhone [brand=lg, model=pro2, price=40000, rating=9],
SmartPhone [brand=MI, model=3s, price=10000, rating=6],
SmartPhone [brand=Letv, model=le2, price=12000, rating=7]]
Sorting the list as comparator
[SmartPhone [brand=MI, model=3s, price=10000, rating=6],
SmartPhone [brand=Letv, model=le2, price=12000, rating=7],
SmartPhone [brand=lg, model=pro2, price=40000, rating=9],
SmartPhone [brand=Apple, model=6s, price=50000, rating=10]]
Reversing the Comparator sorting
Printing the reverse list
[SmartPhone [brand=Apple, model=6s, price=50000, rating=10],
SmartPhone [brand=lg, model=pro2, price=40000, rating=9],
SmartPhone [brand=Letv, model=le2, price=12000, rating=7],
SmartPhone [brand=MI, model=3s, price=10000, rating=6]]