如何使用比较器对 ArrayList 进行排序?
Comparator 是一个接口,用于以排序的方式重新排列 Arraylist。比较器用于对用户定义的对象的 ArrayList 进行排序。在Java中,比较器在Java.util 包中提供。使用 Comparator 我们可以根据多个变量对 ArrayList 进行排序。我们可以简单地实现 Comparator 而不会影响原始的 User-defined 类。要使用 Comparator 对 ArrayList 进行排序,我们需要覆盖比较器接口提供的 compare() 方法。重写 compare() 方法后,我们需要调用 collections.sort() 方法,如下所示。
句法:
Collections.sort(list, comparator)
参数:
- list:应该根据比较器排序的列表。
- 比较器:比较器类实例
返回:它对列表进行排序并且不返回任何内容。
例子
Java
// Java program to Sort ArrayList using Comparator
import java.util.*;
// create the Shop class
class Shop {
int ProductNo;
String name;
int stock;
// constructor
Shop(int ProductNo, String name, int stock)
{
this.ProductNo = ProductNo;
this.name = name;
this.stock = stock;
}
}
// creates the comparator for comparing stock value
class StockComparator implements Comparator {
// override the compare() method
public int compare(Shop s1, Shop s2)
{
if (s1.stock == s2.stock)
return 0;
else if (s1.stock > s2.stock)
return 1;
else
return -1;
}
}
class GFG {
public static void main(String[] args)
{
// create the ArrayList object
ArrayList s = new ArrayList();
s.add(new Shop(218, "Pen", 520));
s.add(new Shop(223, "Pencil", 213));
s.add(new Shop(423, "Books", 101));
s.add(new Shop(512, "Toy", 59));
s.add(new Shop(723, "Bottle", 10));
System.out.println("before sorting");
for (Shop shop : s) {
System.out.println(shop.stock + " " + shop.name
+ " " + shop.ProductNo);
}
System.out.println();
System.out.println(
"After sorting(sorted by Stock)");
// call the sort function
Collections.sort(s, new StockComparator());
for (Shop shop : s) {
System.out.println(shop.stock + " " + shop.name
+ " " + shop.ProductNo);
}
}
}
Java
// Java program to Sort ArrayList using Comparator
import java.util.*;
// create the Shop class
class Shop {
int ProductNo;
String name;
int stock;
// constructor
Shop(int ProductNo, String name, int stock)
{
this.ProductNo = ProductNo;
this.name = name;
this.stock = stock;
}
}
// creates the comparator for comparing name
class NameComparator implements Comparator {
// override the compare() method
public int compare(Shop s1, Shop s2)
{
return s1.name.compareTo(s2.name);
}
}
class GFG {
public static void main(String[] args)
{
// create the ArrayList object
ArrayList s = new ArrayList();
s.add(new Shop(218, "Pen", 520));
s.add(new Shop(223, "Pencil", 213));
s.add(new Shop(423, "Books", 101));
s.add(new Shop(512, "Toy", 59));
s.add(new Shop(723, "Bottle", 10));
System.out.println("before sorting");
for (Shop shop : s) {
System.out.println(shop.name + " " + shop.stock
+ " " + shop.ProductNo);
}
System.out.println();
System.out.println("After sorting(sorted by Name)");
// call the sort function
Collections.sort(s, new NameComparator());
for (Shop shop : s) {
System.out.println(shop.name + " " + shop.stock
+ " " + shop.ProductNo);
}
}
}
输出
before sorting
520 Pen 218
213 Pencil 223
101 Books 423
59 Toy 512
10 Bottle 723
After sorting(sorted by Stock)
10 Bottle 723
59 Toy 512
101 Books 423
213 Pencil 223
520 Pen 218
在上面的示例中,我们按可用库存数量对 Shop 类进行排序。我们还可以根据名称和产品编号对其进行排序。让我们根据名称对上面的 ArrayList 进行排序。
示例 2
Java
// Java program to Sort ArrayList using Comparator
import java.util.*;
// create the Shop class
class Shop {
int ProductNo;
String name;
int stock;
// constructor
Shop(int ProductNo, String name, int stock)
{
this.ProductNo = ProductNo;
this.name = name;
this.stock = stock;
}
}
// creates the comparator for comparing name
class NameComparator implements Comparator {
// override the compare() method
public int compare(Shop s1, Shop s2)
{
return s1.name.compareTo(s2.name);
}
}
class GFG {
public static void main(String[] args)
{
// create the ArrayList object
ArrayList s = new ArrayList();
s.add(new Shop(218, "Pen", 520));
s.add(new Shop(223, "Pencil", 213));
s.add(new Shop(423, "Books", 101));
s.add(new Shop(512, "Toy", 59));
s.add(new Shop(723, "Bottle", 10));
System.out.println("before sorting");
for (Shop shop : s) {
System.out.println(shop.name + " " + shop.stock
+ " " + shop.ProductNo);
}
System.out.println();
System.out.println("After sorting(sorted by Name)");
// call the sort function
Collections.sort(s, new NameComparator());
for (Shop shop : s) {
System.out.println(shop.name + " " + shop.stock
+ " " + shop.ProductNo);
}
}
}
输出
before sorting
Pen 520 218
Pencil 213 223
Books 101 423
Toy 59 512
Bottle 10 723
After sorting(sorted by Name)
Books 101 423
Bottle 10 723
Pen 520 218
Pencil 213 223
Toy 59 512