sort and group a java collection

前端 未结 5 438
无人及你
无人及你 2021-02-01 21:17

I have an object which has a name and a score. I would like to sort a collection of such objects so that they are grouped by name and sorted by maximum score in each group (and

5条回答
  •  余生分开走
    2021-02-01 22:00

    public class ScoreComparator implements Comparator
    {
    
      public int compare(Item a, Item b){
    
        if (a.name.equals(b.name){
          return a.score.compareTo(b.score);
        }
    
        return a.name.compareTo(b.Name);    
    
      }
    
    }
    

提交回复
热议问题