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