Problem overview
This version of the sort tool adds numeric order, like the -n flag. The method sortLines(lines) receives an array of strings. Each string is the text of an integer that fits in 32 bits: an optional leading -, no +, and possibly leading zeros such as "007". The method sorts the lines by numeric value, ascending, and returns the original strings joined by \n.
Lines with the same value keep their original relative order, which is called a stable sort. An empty array gives "".
For ["100", "20", "3", "10"] the result is "3\n10\n20\n100". A text sort would put "100" before "20", because the character "1" is smaller than "2". Numeric order needs the strings turned into numbers for the comparison, while the output must still contain the original strings.
Approach: a comparator that compares numbers
sort() accepts a comparator, a function of two elements a and b that returns a number. A negative result places a first, a positive one places b first, and zero means the two are tied. For numbers, subtraction gives exactly that: a - b is negative when a is smaller.
The elements are strings, so each one is converted inside the comparator. A unary plus in front of a string turns it into a number: +"007" is 7 and +"-5" is -5. The array still holds the strings, so the output keeps "007" as written.
Stability comes from the language: since ES2019, Array.prototype.sort is required to be stable, so elements that compare as 0 stay in their input order, and Node 22 follows that. The comparator does not need a tie-breaker.
Step by step
- Sort a copy with a comparator. Spread
linesinto a new array and callsortwith an arrow function ofaandbthat returns+a - +b. The copy keeps the caller's array in its original order. - Join and return.
join("\n")puts a newline between each two lines, with none after the last.
The solution in JavaScript
class Solution {
sortLines(lines) {
const sortedLines = [...lines].sort((a, b) => +a - +b);
return sortedLines.join("\n");
}
}
The details that matter:
- Unary plus.
+a - +breads as(+a) - (+b). Without the pluses,a - bwould also convert the strings, because the binary minus always works on numbers, but the explicit form shows that the conversion is intended. Usinga + bwould concatenate instead. - Leading zeros are harmless.
+"007"is7, and+"06"is6. They are read as decimal, not octal. - The default sort is textual.
["100", "20", "3", "10"].sort()returns["10", "100", "20", "3"], which is why a comparator is needed. - Why subtraction is safe here. The values fit in 32 bits, and JavaScript numbers are doubles, so the difference of two of them is exact. Subtraction could not overflow a number in this range.
- Bad input gives
NaN.+"abc"isNaN, and a comparator that returnsNaNmakes the order unreliable. The task promises integer strings, so this cannot happen in the lesson.
Complexity
Time: O(n log n) comparisons for n lines. Each comparison converts two short strings, which is cheap. The final join is linear in the text.
Space: O(n) for the copied array and the output.
Edge cases and common mistakes
- Equal values with different text.
["007", "7", "-5", "7", "06", "6"]gives"-5\n06\n6\n007\n7\n7". The value 6 appears as"06"then"6", and the value 7 as"007","7","7": each tie keeps its input order. A sort that compared the text to break ties would produce a different result. - Negative numbers.
["-1", "-20", "3"]gives"-20\n-1\n3". Sorted as text,"-1"would come before"-20", because"1"is smaller than"2", even though -20 is the smaller number. - Forgetting the comparator. The code runs and returns a plausible-looking but wrong order.
- Returning a boolean. A comparator such as
(a, b) => +a > +breturnstrueorfalse, which become 1 and 0 and never ask forato go first. On["3", "1", "2", "1"]that comparator leaves the array unsorted. - Replacing the strings with numbers. The output must keep the original text, such as
"007", so convert only inside the comparator. - Empty array. It returns
"".
Testing
["100", "20", "3", "10"]gives"3\n10\n20\n100".["-1", "-20", "3"]gives"-20\n-1\n3".["007", "7", "-5", "7", "06", "6"]gives"-5\n06\n6\n007\n7\n7".[]gives"".
