Sort by

recency

|

265 Discussions

|

  • + 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();
    }
    

    }

  • + 0 comments

    import java.io.; import java.util.; import java.util.List; import java.util.ArrayList; class Student{ public String name; public int score; public Student(String name,int score){ this.name = name; this.score = score; }

    } class Sortbyscore implements Comparator{ public int compare(Student a,Student b){ if(a.score == b.score){ return a.name.compareTo(b.name); } return b.score - a.score; } } public class Solution {

    public static void main(String[] args) {
     ArrayList<Student> student = new ArrayList<Student>();
     Scanner scan = new Scanner(System.in);
     int n = scan.nextInt();
     for(int i=0;i<n;i++){
         String names = scan.next();
         int scores = scan.nextInt();
         student.add(new Student(names,scores));
    
     }
     Collections.sort(student,new Sortbyscore());
     for(int i=0;i<student.size();i++){
         System.out.println(student.get(i).name+" "+ student.get(i).score);
     }
    }
    

    }