sort and group a java collection

前端 未结 5 448
无人及你
无人及你 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 21:59

    Yes Go for Comparator

    Give first preference to name in comparison and then to score. it will be grouped up with sorted score also

        List scores = new ArrayList();
        scores.add(new Score("a", 58));
        scores.add(new Score("a", 10));
        scores.add(new Score("b", 165));
        scores.add(new Score("a", 1));
        scores.add(new Score("b", 1658));
        scores.add(new Score("c", 1));
        scores.add(new Score("c", 10));
        scores.add(new Score("c", 0));
    
        Collections.sort(scores, new Comparator() {
    
            public int compare(Score o1, Score o2) {
                if (o1.getName().compareTo(o2.getName()) == 0) {
                    return o2.getScore() - o1.getScore();
                } else {
                    return o1.getName().compareTo(o2.getName());
                }
            }
        });
        System.out.println(scores);
    

    Update

    As Chris pointed out.

    import java.util.*;
    
    /**
     *
     * @author Jigar
     */
    class Score {
    
        private String name;
        private List scores;
    
        public Score() {
        }
    
        public Score(String name, List scores) {
            this.name = name;
            this.scores = scores;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List getScores() {
            return scores;
        }
    
        public void setScores(List scores) {
            this.scores = scores;
        }
    
        @Override
        public String toString() {
            return name + " , " + scores + "\n";
        }
    }
    
    public class ScoreDemo { 
    
        public static void main(String[] args) {
            List scores = new ArrayList();
    
    
            List lstA = new ArrayList();
            lstA.add(3);
            lstA.add(9);
            lstA.add(7);
            Collections.sort(lstA);
            Collections.reverse(lstA);
    
            List lstB = new ArrayList();
            lstB.add(10);
            lstB.add(8);
            lstB.add(3);
            Collections.sort(lstB);
            Collections.reverse(lstB);
    
            List lstC = new ArrayList();
            lstC.add(8);
            lstC.add(3);
            Collections.sort(lstC);
            Collections.reverse(lstC);
    
    
            scores.add(new Score("a", lstA));
            scores.add(new Score("b", lstB));
            scores.add(new Score("c", lstC));
    
    
    
    
    
            Collections.sort(scores, new Comparator() {
    
                public int compare(Score o1, Score o2) {
                    return o2.getScores().get(0).compareTo(o1.getScores().get(0));
                }
            });
            System.out.println(scores);
    
        }
    }
    
    • Here is working ideone demo

    • UPDATE: Here is working ideone demo

提交回复
热议问题