In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "how to understand the use of the sort method of Java sorting". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Catalogue
Introduction
Ascending order
Array
Set
Descending order
Declare a class to implement the interface
Anonymous inner class implementation interface
Lambda expression implementation interface
Sort custom data types
Summary:
Introduction
In the process of learning Java, sorting sort is a common function; in Java, arrays can be sorted by Arrays.sort (), and collections are sorted by the Collections.sort () method; by default, ascending sorting, but how to sort descending? Which methods can be used? What about custom types?
Let's introduce the use of the sort method.
Ascending order
Ascending order is by default, so here is a brief demonstration of the methods used
Array
The sort method of the array is located under the Arrays utility class
The code is as follows:
Import java.util.Arrays;public class SortTest01 {public static void main (String [] args) {int [] array = {2, 4, 8, 9, 78, 1, 3, 66}; / integer array Arrays.sort (array); / / ascending sort for (int I: array) {System.out.print (I + ");}
The results are as follows:
1 2 3 4 8 9 66 78
Set
The sort method of the collection is located under the Collections class
The code is as follows:
Import java.util.Collections;import java.util.LinkedList;import java.util.List;public class SortTest02 {public static void main (String [] args) {List list = new LinkedList (); / define a linked list / / put in list for in descending order (int I = 10; I > = 0;-- I) {list.add (I);} Collections.sort (list) / / sort for in ascending order (var I: list) {System.out.print (I + "");}
The results are as follows:
0 1 2 3 4 5 6 7 8 9 10
Descending order
Descending requires a few extra steps; an interface is needed here: Comparator
In fact, what we use is only a method in the Comparator interface, and it is also the only method of this interface: int compare (T _ 1, T _ 2)
The sort method is ascending by default, but an overloaded version of it can put an extra parameter of Comparator as a comparator
Take a look at the sort method of Collections:
And the sort method in Arrays.
You can see that they are all Comparator parameters, so we just need to make the specified rules here, and we can sort them our way.
So the next question is how to implement the Comparator interface and rewrite the int compare (T _ 1, T _ 2) method. Here I provide three methods:
Let's just use sets as an example.
Declare a class to implement the interface
The code is as follows:
Import java.util.Collections;import java.util.Comparator;import java.util.LinkedList;import java.util.List;public class SortTest03 {public static void main (String [] args) {List list = new LinkedList (); / / define a linked list / / put in list for (int I = 0; I) in ascending order
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.