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

Java comes with sorting how to use it

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use java's own ranking, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let's take a look at it.

Basic type sort:

Int a [] = {1, 2, 5, 5, 3, 6, and 4}; Arrays.sort (a); / / a pair of a sort in ascending order Arrays.sort (aline 0, 6); / / a pair of a, sort from subscript 0 to subscript 5.

Custom type sorting:

Static Student b [] = new Student [4]; for (int item0 TIo2-returns a positive number

People are dizzy.

Let's get this straight:

Sort functions sort from small to large by default

So how to define who is big and who is small? give the sort function a comparison function.

So the comparison function already has two parameters, how to express the size relationship of o1 ~ 2?

The logic is as follows:

O1

< o2 ---- 返回负数 o1 = o2 ---- 返回 0 o1 >

O2-returns a positive number

Returns a negative number to represent o1o2

To separate the sorting function from the comparison function

They are independent functions, as long as they make sure that they are correct.

What you need to do to write a comparison function is:

Returns an integer on o1o2

The "" is not necessarily less than and greater than in the real numerical sense, but what you want to be less and greater.

For example, o1o2 is int.

If (o1 > O2) return-1

So the value of o1 is larger than O2, which is what I define as "less than".

Thank you for reading this article carefully. I hope the article "how to use java self-sorting" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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