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 use Collections.sort and Comparator in List sorting

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use Collections.sort and Comparator for List sorting. I hope you will get something after reading this article. Let's discuss it together.

/ * *

* example of list sort

* @ param schedules

* @ param schoolId

* @ return boolean

, /

Public boolean checkCourseTimes (List schedules, Long schoolId) {

Boolean flag=false

Collections.sort (schedules, new Comparator () {

Public int compare (Object a, Object b) {

Schedule cs1 = (Schedule) a

Schedule cs2 = (Schedule) b

Date cs1date=cs1.getStartTime ()

Date cs2date=cs2.getStartTime ()

Int temp=cs1date.compareTo (cs2date)

Return temp

}

});

Return flag

}

The other one

/ * *

* Recursively sort the tree

*

* @ param channelCsTypes

, /

Private void sort (List channelCsTypes, Comparator compara) {

If (channelCsTypes = = null | | channelCsTypes.size () < 1)

Return

For (int I = 0; I < channelCsTypes.size (); iTunes +) {

List subs = ((ChannelCstype) channelCsTypes.get (I)) .getSubTypes ()

Sort (subs, compara)

}

Collections.sort (channelCsTypes, compara)

}

Public List loadAllCstypesByChannel (String channel) {

List result = new ArrayList ()

/ / Todo orgernize the list

/ / sort the types

Sort (result, new Comparator () {

Public int compare (Object a, Object b) {

ChannelCstype cs1 = (ChannelCstype) a

ChannelCstype cs2 = (ChannelCstype) b

Int S1 = cs1.getSort ()! = null? Cs1.getSort () .intValue (): 0

Int S2 = cs2.getSort ()! = null? Cs2.getSort () .intValue (): 0

Return S1-S2

}

});

Return result

}

After reading this article, I believe you have a certain understanding of "how to use Collections.sort and Comparator for List sorting". If you want to know more about it, welcome to follow the industry information channel. Thank you for reading!

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