Sort by

recency

|

266 Discussions

|

  • + 0 comments

    class Checker implements Comparator{

    @Override
    public int compare(Player p1, Player p2){
        int scoreCompare = p2.score - p1.score; 
        int nameCompare = p1.name.compareTo(p2.name);
        return (scoreCompare==0) ? nameCompare:scoreCompare; 
    }
    

    }

  • + 0 comments
    class Checker implements Comparator<Player> {
        public int compare(Player p1, Player p2) {
            int scoreComparator = Integer.compare(p2.score, p1.score);
            if (scoreComparator != 0) {
                return scoreComparator;
            } else {
                return p1.name.compareTo(p2.name);
            }
        }
    }
    
  • + 0 comments
    public int compare(Player playerA, Player playerB) {
    
            if(playerB.score == playerA.score) {
                return playerA.name.compareTo(playerB.name);
            } else if (playerB.score > playerA.score) {
                return 1;
            } else {
                return -1;
            }
        }
    
  • + 0 comments
    class Checker implements Comparator<Player>{
        public int compare(Player p1,Player p2){
            int byScore = Integer.compare(p2.score, p1.score);
            if (byScore == 0) return p1.name.compareTo(p2.name); 
            return byScore;
        }
        
    }
    
  • + 0 comments

    // import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
      Map<String ,Integer> map=new HashMap<>();
      Scanner sc=new Scanner(System.in);
      int n=sc.nextInt();
    
      for(int i=0;i<n;i++){
          String name=sc.next();
          Integer score=sc.nextInt();
          map.put(name, score);
    
      }
       List<Map.Entry<String, Integer>> entryList = new ArrayList<>(map.entrySet());
    
    
        Collections.sort(entryList, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                if(o1.getValue().equals(o2.getValue())){
                     return o1.getKey().compareTo(o2.getKey());
                }
                else if(o1.getValue().equals(o2.getValue())){
                    return o1.getValue().compareTo(o2.getValue());
                }
    
               return -o1.getValue().compareTo(o2.getValue());
            }
        });
        Map<String, Integer> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : entryList) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }
    
    
        for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
            System.out.println(entry.getKey() +" "+ entry.getValue());
        }
      sc.close();
    }
    

    }