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.
- Prepare
- Algorithms
- Sorting
- Big Sorting
- Discussions
Big Sorting
Big Sorting
Sort by
recency
|
931 Discussions
|
Please Login in order to post a comment
Java
Java8
Collections.sort(unsorted, (a, b) -> { // Compare by length first int lenA = a.length(); int lenB = b.length(); if (lenA != lenB) { return lenA - lenB; } // If lengths are equal, compare lexicographically return a.compareTo(b); }); return unsorted;
Try this def bigSorting(unsorted): return sorted(unsorted, key=lambda x: (len(x), x))
This solution sorts the strings based on two criteria: The length of the string (shorter strings come first) The lexicographical order of the string itself
It avoids converting strings to integers, which can be slow for very large numbers. It handles numbers of different lengths correctly without conversion. It's faster for comparing very large numbers, as it stops comparing as soon as it finds a difference.
Here is my c++ solution, you can watch the explanation here : https://youtu.be/GAvltofMdYc