UK London US New York US Birmingham UK BirminghamSimilarly, stable sorting on just the first column would generate “UK London” as the first item and “US Birmingham” as the last item (since the order of the elements having the same first word – “UK” or “US” – would be maintained). Write a function that takes a 2D array as a parameter. Each element has 2 elements similar to the above example. The function should sort the array as mentioned previously and return the sorted array.
stableSort
should be a function.
testString: assert(typeof stableSort == 'function', 'stableSort
should be a function.');
- text: stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])
should return a array.
testString: assert(Array.isArray(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])), 'stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])
should return a array.');
- text: stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])
should return [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]
.
testString: assert.deepEqual(stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]), [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]], 'stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]])
should return [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]]
.');
- text: stableSort([[2, 2], [1, 2], [1, 4], [1, 5]])
should return [[2, 2], [1, 2], [1, 4], [1, 5]]
.
testString: assert.deepEqual(stableSort([[2, 2], [1, 2], [1, 4], [1, 5]]), [[2, 2], [1, 2], [1, 4], [1, 5]], 'stableSort([[2, 2], [1, 2], [1, 4], [1, 5]])
should return [[2, 2], [1, 2], [1, 4], [1, 5]]
.');
- text: stableSort([[11, 55], [12, 45], [11, 45], [32, 45]])
should return [[12, 45], [11, 45], [32, 45], [11, 55]]
.
testString: assert.deepEqual(stableSort([[11, 55], [12, 45], [11, 45], [32, 45]]), [[12, 45], [11, 45], [32, 45], [11, 55]], 'stableSort([[11, 55], [12, 45], [11, 45], [32, 45]])
should return [[12, 45], [11, 45], [32, 45], [11, 55]]
.');
- text: stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]])
should return [[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]]
.
testString: assert.deepEqual(stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]]), [[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]], 'stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]])
should return [[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]]
.');
- text: stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]])
should return [[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]]
.
testString: assert.deepEqual(stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]]), [[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]], 'stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]])
should return [[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]]
.');
```