Sorting highscore . Diffuculties with sorting time input

前端 未结 1 1009
鱼传尺愫
鱼传尺愫 2021-01-29 10:07

I\'m having diffuculties with sorting the input i want it to sort by lowest time first. I\'m new to java so i dont know so much I\'ve done a guees a number game but I cant manag

相关标签:
1条回答
  • 2021-01-29 10:37

    Create a value object that holds all your scoring, time and other details. Create a Comparator that compares the components between two of these holders. The sorting can be achieved by creating a Set of Holder with a Comparator.

    If you wish to sort by other properties in a different order simply update the Comparator as appropriate.

    Holder {
        long time;
        int score;
    
        String name;
    }
    
    Comparator<Holder> {
        int compare( Holder holder, Holder other ){
           int result = holder.time - other.time;
           if( 0 == result ){
              result = holder.score - other.score;
           }
            return result;
       }
    }
    
    0 讨论(0)
提交回复
热议问题