Home > Web Front-end > JS Tutorial > A brief discussion on the sorting method of List

A brief discussion on the sorting method of List

little bottle
Release: 2019-04-24 15:05:46
forward
3777 people have browsed it

The editor of this article will talk to you about the sorting method of List. Friends who are interested can come and learn about it. I hope it will be helpful to you.

You can use Collections.sort(List list) and Collections.sort(List list, Comparator c). In fact, both methods are required for sorting. Comparators.

The former method is sorted according to the internal comparator, that is, the elements in the List must implement the Comparable interface. This interface has a compareTo method to compare a certain attribute value of two objects.

The latter method is to sort according to the external comparator of the Comparator interface implementation class. The Comparator interface has a compare method for comparing a certain attribute value of two objects.

The comparison results all return int type, with only 3 values, namely "-1" in ascending order, "0" in ascending order, "1" in descending order

Java8 post-List The object itself has a sort() method for sorting, which actually uses the above implementation principle, but it uses the lambda expression lambda of Java 8, making the code more concise to write.

Related tutorials: java video tutorial

The above is the detailed content of A brief discussion on the sorting method of List. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template