JAVA sorts list collectionCollections.sort()
Sort the objects in a collection in ascending or descending order according to the size of a certain indicator of the object. The code is as follows:
Arrange in descending order
进行降序排列 Collections.sort(list, new Comparator<ResultTypeDesc>() { public int compare(ResultTypeDesc o1, ResultTypeDesc o2) { return o2.getRatio().compareTo(o1.getRatio()); } });
Arrange in ascending order
Collections.sort(list, new Comparator<ResultTypeDesc>() { public int compare(ResultTypeDesc o1, ResultTypeDesc o2) { return o1.getRatio().compareTo(o2.getRatio()); } });
After testing, it was found that only two The position of the objects can be changed in ascending or descending order.
If the indicators are the same, to sort based on multiple indicators, you need to create a comparator:
import java.util.*; public class ComparatorResultType implements Comparator{ public int compare(Object arg0, Object arg1) { ResultTypeDesc desc0=(ResultTypeDesc)arg0; ResultTypeDesc desc1=(ResultTypeDesc)arg1; //首先比较主指标,如果主指标相同,则比较次指标 int flag=desc0.getXXX().compareTo(desc1.getXXX()); if(flag==0){ return desc0.getXXX2().compareTo(desc1.getXXX2()); }else{ return flag; } } } //测试类中代码: ComparatorResultType comparator=new ComparatorResultType(); Collections.sort(list, comparator);
Inverse output of the list collection:
Collections .reverse(list);
ResultTypeDesc is the required entity class object. The specific use can be combined with your own code.
This method may report a null pointer. You can solve it yourself based on the situation and determine whether it is NULL.
For more JAVA sorting list collections Collections.sort() related articles, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...
