Home > Java > Java Tutorial > body text

How does Arrays.sort() method in Java sort arrays by custom comparator?

王林
Release: 2023-11-18 11:36:57
Original
829 people have browsed it

How does Arrays.sort() method in Java sort arrays by custom comparator?

How does the Arrays.sort() method in Java sort an array according to a custom comparator?

In Java, the Arrays.sort() method is a very useful method for sorting arrays. By default, this method sorts in ascending order. But sometimes, we need to sort the array according to our own defined rules. At this time, you need to use a custom comparator (Comparator).

A custom comparator is a class that implements the Comparator interface, which can define comparison rules according to specific needs. Custom comparators can sort objects of any type, including basic types and custom types.

Next, I will introduce how to use a custom comparator to sort the Arrays.sort() method in Java and provide some sample code to illustrate.

  1. How to define a custom comparator?

To define a custom comparator, you only need to implement the Comparator interface and override the compare method. The compare method has two parameters. We need to define comparison rules in this method. The comparison rules have the following situations:

  • obj1
  • obj1 == obj2, returns 0;
  • obj1 > obj2, returns a positive integer;

The key code is as follows:

public class MyComparator implements Comparator {
    @Override
    public int compare(T o1, T o2) {
        // 你的自定义比较规则
        return 0;
    } 
}
Copy after login
  1. How to use custom comparison to sort an array?

The Arrays.sort() method has multiple overloaded versions, one of which accepts a custom comparator. We can use our own defined comparator as the second parameter of this method, so that we can sort the array according to our own rules.

The key code is as follows:

public static  void sort(T[] a, Comparator c) 
Copy after login

Among them, T[] a represents the array to be sorted, Comparator c represents the comparator.

  1. Sample code

Now, let's look at a specific example. Suppose we have a Student class. This class contains two member variables: name and age. Now, if we want to sort the Student array from smallest to largest age, we need to define a custom comparator. The sample code is as follows:

public class StudentComparator implements Comparator {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    } 
}
Copy after login

In the above code, we define a StudentComparator class, which implements the Comparator interface and overrides the compare method. In the compare method, we focus the comparison on age. If o1's age is less than o2's age, a negative integer is returned; if o1's age is equal to o2's age, 0 is returned; if o1's age is greater than o2's age, a positive integer is returned.

Next, we can use the Arrays.sort() method to sort the Student array according to the rules we define.

public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[3];
        students[0] = new Student("Tom", 20);
        students[1] = new Student("Jack", 18);
        students[2] = new Student("Lucy", 22);
        
        Arrays.sort(students, new StudentComparator());
        
        for (Student student : students) {
            System.out.println(student.name + " " + student.age);
        }
    }
}
Copy after login

The output result is:

Jack 18
Tom 20
Lucy 22
Copy after login

It can be seen from the result that according to the rules we defined, the Student array is sorted from small to large by age.

  1. Summary

The Arrays.sort() method is a very useful method that can sort any type of array. When you need to sort the array according to your own defined rules, you only need to implement the Comparator interface and rewrite the compare method. The specific implementation process is as mentioned above, I hope it will be helpful to everyone.

The above is the detailed content of How does Arrays.sort() method in Java sort arrays by custom comparator?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!