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
little bottleforward
2019-04-24 15:05:463805browse

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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete