One of the steps in the merge-sort algorithm is merging two sorted lists together. In the spirit of doing everything on a single line (see my other posts), here is an admittedly hokey one-liner for merging two lists together (it creates a new merged list and clobbers the second of the two original sorted lists):
const orderedList1 = [1,3,5,7,9]; const orderedList2 = [0,2,5,8,11]; console.log([...orderedList1.reduce((a, e) => [...orderedList2.some(r => e > r? a.push(orderedList2.shift()): false)?a:a, e], ), ...orderedList2]); // [ 0, 1, 2, 3, 5, 5, 7, 8, 9, 11 ]
A bit of an explanation for this monstrosity:
The outer "reduce" starts with an empty list, goes through each element of the first list, extracts all the elements of the second list that are ahead of the current element of the first list and inserts them into the merged list, and then inserts the current element. Finally, the remainder of the second list, if any, is appended at the end of the merged list. The fake ternary operator smuggles in the "some" call inside the spread operator collecting the merged list in progress. Not sure if this can be done more neatly.
(open source and trusted by devs everywhere ❤️)