Skip to main content
3 of 5
added 315 characters in body
edc65
  • 32.2k
  • 3
  • 36
  • 90

#JavaScript (ES6) 54 72

That's easier than it seems. Just sort them lexicographically. The good news is: that's exactly how plain javascript sort works.Well ... no, that's wrong ... still a (more convoluted) lexicograph compare can do the job.

Note: having a and b numeric, a+[b] is a shortcut for a+''+b, as we need a string concatenation and not a sum.

F=l=>(l.sort((a,b)=>a+[b]>b+[a]).join('')+' '+l.reverse().join(''))

Test In Firefox / FireBug console

F([50, 2, 1, 9])
F([5,56,50])
F([52,36,526])
F([52,36,525])
F([52,36,524]

12509 95021
50556 56550
3652526 5265236
3652525 5255236
3652452 5252436

edc65
  • 32.2k
  • 3
  • 36
  • 90