We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Integers don't sort correctly when viewed as strings because, for example, '2' as a string is less than '15' due to lexicographic ordering, and converting the giant strings to integers seemed to slow things to a crawl. But integers of the same size do sort correctly as strings, so I sorted first by length and then broke "ties" by lexicographic sorting.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Big Sorting
You are viewing a single comment's thread. Return to all comments →
I ended up working out the following one-line solution in Python 3:
Integers don't sort correctly when viewed as strings because, for example, '2' as a string is less than '15' due to lexicographic ordering, and converting the giant strings to integers seemed to slow things to a crawl. But integers of the same size do sort correctly as strings, so I sorted first by length and then broke "ties" by lexicographic sorting.