How can I manipulate an array to make the largest number?

前端 未结 16 2111
暖寄归人
暖寄归人 2021-01-30 02:47

Say you have an array of positive integers, manipulate them so that the concatenation of the integers of the resultant array is the largest number possible. Ex: {9,1,95,17,5}, r

16条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-30 03:22

    Not sure if anyone is looking for a JavaScript solution, but if you are, here is the code

    function LexicographicSort(input) {
        return Number(
            input
                .sort(function(a, b) {
    // lexicographic sort
                   return Number("" + b + a) - Number("" + a + b);
                })
                .join("")
            );
    }
    

提交回复
热议问题