Sorting: Comparator

  • + 0 comments

    Javascipt

    function processData(input) {
        //Enter your code here
        const arr = input.split("\n")
        const result = []
        for (let i = 1; i < arr.length; i++) {
            result.push(arr[i].split(" "))
        }
        result.sort((a, b) => {
            return b[1] - a[1];
        });
    
        result.sort((a, b) => {
            if (b[1] === a[1] && b[0] > a[0]) {
                return -1;
            }
        });
        console.log(result.join("\n").replaceAll(",", " "));
    
    }