Home > Java > Java Tutorial > body text

Java uses the sort() function of the Collections class to sort collections

WBOY
Release: 2023-07-24 17:01:11
Original
2265 people have browsed it

Java uses the sort() function of the Collections class to sort collections

In Java, we often need to sort collections. The Collections class provides a sort() function that can easily sort collections. This article will introduce how to use the sort() function of the Collections class to sort collections, with code examples.

First, we need to import the java.util package to use the Collections class.

import java.util.Collections;
import java.util.ArrayList;
Copy after login

Next, we create an ArrayList object to sort as a sample collection.

ArrayList numbers = new ArrayList<>();
numbers.add(3);
numbers.add(1);
numbers.add(4);
numbers.add(2);
numbers.add(5);
Copy after login
Copy after login

Now, we can call the sort() function of the Collections class to sort the collection. The sort() function automatically sorts the elements according to their natural order, for example, for integers, they are sorted from small to large.

Collections.sort(numbers);
Copy after login

By printing the collection, we can see that the collection has been sorted from small to large.

System.out.println(numbers);
Copy after login
Copy after login

The output result is: [1, 2, 3, 4, 5].

In addition to sorting using natural order, we can also use custom comparators to sort collections. A comparator is a class that implements the Comparator interface. We need to pass in the comparator object in the sort() function to implement custom sorting.

Here is an example that demonstrates how to use a custom comparator to sort a collection in descending order.

First, we create a comparator class DescendingComparator, implement the Comparator interface, and rewrite the compare() function.

import java.util.Comparator;

class DescendingComparator implements Comparator {
    @Override
    public int compare(Integer o1, Integer o2) {
        return o2.compareTo(o1);
    }
}
Copy after login

Then, we create an ArrayList object and add elements using the add() function.

ArrayList numbers = new ArrayList<>();
numbers.add(3);
numbers.add(1);
numbers.add(4);
numbers.add(2);
numbers.add(5);
Copy after login
Copy after login

Now, we can call the sort() function of the Collections class and pass in the DescendingComparator object to sort in descending order.

Collections.sort(numbers, new DescendingComparator());
Copy after login

By printing the collection, we can see that the collection has been sorted in descending order.

System.out.println(numbers);
Copy after login
Copy after login

The output result is: [5, 4, 3, 2, 1].

In this article, we learned how to sort a collection using the sort() function of the Collections class. We can use natural ordering or custom comparators to achieve different sorting methods. By flexibly using the sort() function, we can sort the collection more easily.

I hope this article will help you understand collection sorting in Java!

The above is the detailed content of Java uses the sort() function of the Collections class to sort collections. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!