Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Sorting algorithms

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Sorting algorithms

Largest Number

intermediatelayer B7 · Sorting algorithms5 tests

Given an array of non-negative integers, arrange them so that concatenating their decimal digits gives the largest possible number. Return it as a string.

  • [10, 2] gives '210', because 210 > 102
  • Sorting numerically or lexicographically both give the wrong answer — you need a custom comparator
  • The result can be far longer than a JavaScript number can hold, which is why it is a string
  • Edge case: all zeros must return '0', not '000'
  • An empty array returns ''

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen