-7

The task is to make a function that can take any non-negative integer as an argument and return it with its digits in descending order. Essentially, rearrange the digits to create the highest possible number.

Examples:

Given Input: 21445 -> Expected Output: 54421

Given Input: 145263 -> Expected Output: 654321

Given Input: 1254859723 -> Expected Output: 9875543221

My thinking is to break the number into individual strings and push into an array then sort them descending way then make it a number again. But is there a better way to handle this?

4
  • You answered your own question "My thinking is to break the number into individual strings and push into array then sort them descending way then make it a number again " Commented Aug 14, 2018 at 18:37
  • +num.toString().match(/\d/g).sort((a,b)=>b>a).join('') Commented Aug 14, 2018 at 18:48
  • +number.toString().split("").sort((p,c)=>p<c).join("");
    – Emeeus
    Commented Aug 14, 2018 at 18:50
  • 1
    @epascarello This is my first question in StackOverflow, i didn't know whats the etiquette here and its not home work its a coding challenge i am trying to crack in codewars.And as stated in my question i wanted a better way to do it rather than break and sort and rejoin.
    – Kumar19PD
    Commented Aug 14, 2018 at 19:13

6 Answers 6

3

You can split the String on "" and map it to convert each item to a number. Then you can use Array.prototype.sort to put it in descending order. If you want it as a String you can join it on "".

var str = "92345";
var numStr = str.split('').map(function(item) {
    return +item;
});
var orderedArr = numStr.sort(function(a,b){
 return b-a;
});
var orderedStr = orderedArr.join("");
console.log(orderedArr);
console.log(orderedStr);

A shorter way would be to use the unary operator to convert the String to a number, split it on "", sort it, and join it on "".

var str = "91785";
function orderDesc(str){
  return +str.split('').sort((a,b)=>b-a).join('');
}
console.log(orderDesc(str));

2
  • just use a urnary plus .map(x=>+x) but in the end sort of strings/numbers will not make a real difference Commented Aug 14, 2018 at 18:47
  • @Kumar19PD No problem. Commented Aug 14, 2018 at 20:36
0

I would say that is the way I would do it. But, be sure to implement Array.sort() properly. It is not a stable sort. You may get different results depending on the engine (V8, SpiderMonkey, Chakra).

0

I wouldn't convert to string, as it's counter intuitive, you're working with numbers, so I'd recommend to stick to them.

Can't tell if my approach has better performance, but there are ways to track it, such as millis counter, and memory allocation watches.

while (number > 0) {
  array.push(number % 10);
  number /= 10;
}

array.sort();

foreach (val in array) {
  number += val;
  number *= 10;
}

return number;
2
  • Can you add a working snippet? Because copying your function to test, it didn't work for me. Also, I'm not sure, but I think that foreach() like used here, doesn't exist in JS Commented Aug 14, 2018 at 18:45
  • @CalvinNunes That's called pseudo-code. Something that gives you an exact explanation of the procedure, but requires some tuning to adapt it to your development environment.
    – Dane411
    Commented Aug 14, 2018 at 18:50
0
num = 234564
num1 = str(num)
list1 = list(num1)
list2 = sorted(list1, reverse=True)
string_1 = ''.join(list2)
final_integer = int(string_1)
return final_integer
1
  • could you share the output of the code to help the answer read better?
    – jmp
    Commented Jun 23, 2020 at 2:08
0
function descendingOrder(n){

    var n = n.toString().split('');
    var descendingNumber = n.sort((a, b)=> b-a).join('');
    console.log("The Descending Number is =",descendingNumber);
}
descendingOrder(13245);
1
  • 1
    Make sure you add an explanation to your code for a better answer :D
    – M Z
    Commented Apr 1, 2021 at 20:33
0

Finall solution would be this, but we just need to put the string back to Number.

function descendingOrder(n){

    var n = n.toString().split('');
    var descendingNumber = Number(n.sort((a, b)=> b-a).join(''));
    
}
descendingOrder(13245);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.