Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the use of sort method of Java sorting

2025-01-16 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report