Multi criteria sorting of a list of objects with Guava Ordering

前端 未结 3 888
[愿得一人]
[愿得一人] 2021-02-13 23:30

I have a class WHICH CANNOT implement comparable, but needs to be sorted based on 2 fields. How can I achieve this with Guava?

Let\'s say the class is:

c         


        
3条回答
  •  我在风中等你
    2021-02-14 00:15

    I suspect you want Ordering.compound. You could do it all in one statement, but I'd use:

    Ordering primary = Ordering.natural().onResultOf(stringValueSortFunction);
    Ordering secondary = Ordering.natural()
                                  .onResultOf(dateValueSortFunction)
                                  .reverse();
    Ordering compound = primary.compound(secondary);
    
    List sortedList = compound.immutableSortedCopy(lotsOfX);
    

提交回复
热议问题