Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the use of sort method of Java sorting

Shulou Source: shulou.com Published: 2022-06-03 08:49:19 09月28日 Update

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

Tags: Sort method interface ascending order code result rule case array expression or type actual age Zhang San Li Si Wang Wu practical next example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Microsoft MySQL Shulou Information MariaDB