Use collections. In Java collections How the sort method sorts the list (two methods)

The first is that the objects in the list implement the comparable interface, as follows:

Test:

The output results are as follows

a b

The second method is based on collections Sort overload method, for example:

Write this in the main class:

The output results are as follows

a b

The former has a simple code structure, but can only be sorted according to fixed attributes. The latter is flexible and can temporarily specify sorting items, but the code is not concise enough

Multi field situations:

The above content is a small series to introduce you about using collections in Java collections Sort method how to sort the list (two methods), I hope it will be helpful to you.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>