Given a string input of a company id and rating, sort them by rating in descending order.


  • 0
    V

    input ="1005 2\n1001 5\n1002 5\n1004 1"

    var companyRating = function(input) {

    };


  • 0
    M
    This post is deleted!

  • 0
    H
            String input = "1005 2\n1001 5\n1002 5\n1004 1";
            String[] arr = input.split("\n");
    
            Arrays.sort(arr,new Comparator<String>(){
                @Override
                public int compare(String s1, String s2){
                    String[] s1Arr = s1.split(" ");
                    String[] s2Arr = s2.split(" ");
    
                    return Integer.valueOf(s1Arr[1]) - Integer.valueOf(s2Arr[1]);
                }
            });
    
            for (String s : arr)
                System.out.println(s);
    

  • 0
    W

    @hikkiii_p It should be displaying in descending order, thus the compare function should be fixed a little bit.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.