3.6 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 5a23c84252665b21eecc8014 | Stabilità dell'ordinamento | 1 | 302308 | sort-stability |
--description--
When sorting records in a table by a particular column or field, a stable sort will always retain the relative order of records that have the same key.
Ad esempio, in questa tabella di paesi e città, un ordinamento stabile sulla seconda colonna, le città, manterrebbe US Birmingham sopra UK Birmingham. (Anche se un ordinamento instabile potrebbe, in questo caso, posizionare US Birmingham sopra UK Birmingham, un ordinamento stabile lo garantirebbe esso).
UK London US New York US Birmingham UK Birmingham
Allo stesso modo, l'ordinamento stabile fatto solo sulla prima colonna genererebbe "UK London" come primo elemento e "US Birmingham" come ultimo elemento (perché l'ordine degli elementi con la stessa prima parola – "UK" o "US" – sarebbe mantenuto).
--instructions--
Scrivi una funzione che richiede un array bidimensionale come parametro. Ogni elemento ha 2 elementi simili all'esempio precedente. La funzione dovrebbe ordinare l'array come menzionato precedentemente e restituire l'array ordinato.
--hints--
stableSort dovrebbe essere una funzione.
assert(typeof stableSort == 'function');
stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]) dovrebbe restituire un array.
assert(
Array.isArray(
stableSort([
['UK', 'London'],
['US', 'New York'],
['US', 'Birmingham'],
['UK', 'Birmingham']
])
)
);
stableSort([["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"]]) dovrebbe restituire [["US", "Birmingham"], ["UK", "Birmingham"], ["UK", "London"], ["US", "New York"]].
assert.deepEqual(
stableSort([
['UK', 'London'],
['US', 'New York'],
['US', 'Birmingham'],
['UK', 'Birmingham']
]),
[
['US', 'Birmingham'],
['UK', 'Birmingham'],
['UK', 'London'],
['US', 'New York']
]
);
stableSort([[2, 2], [1, 2], [1, 4], [1, 5]]) dovrebbe restituire [[2, 2], [1, 2], [1, 4], [1, 5]].
assert.deepEqual(
stableSort([
[2, 2],
[1, 2],
[1, 4],
[1, 5]
]),
[
[2, 2],
[1, 2],
[1, 4],
[1, 5]
]
);
stableSort([[11, 55], [12, 45], [11, 45], [32, 45]]) dovrebbe restituire [[12, 45], [11, 45], [32, 45], [11, 55]].
assert.deepEqual(
stableSort([
[11, 55],
[12, 45],
[11, 45],
[32, 45]
]),
[
[12, 45],
[11, 45],
[32, 45],
[11, 55]
]
);
stableSort([[10, 22], [1, 2], [1, 4], [1, 5], [10, 9]]) dovrebbe restituire [[1, 2], [1, 4], [1, 5], [10, 9], [10, 22]].
assert.deepEqual(
stableSort([
[10, 22],
[1, 2],
[1, 4],
[1, 5],
[10, 9]
]),
[
[1, 2],
[1, 4],
[1, 5],
[10, 9],
[10, 22]
]
);
stableSort([[55, 54], [12, 22], [31, 43], [31, 54], [10, 49]]) dovrebbe restituire [[12, 22], [31, 43], [10, 49], [55, 54], [31, 54]].
assert.deepEqual(
stableSort([
[55, 54],
[12, 22],
[31, 43],
[31, 54],
[10, 49]
]),
[
[12, 22],
[31, 43],
[10, 49],
[55, 54],
[31, 54]
]
);
--seed--
--seed-contents--
function stableSort(arr) {
}
--solutions--
function stableSort(arr) {
arr.sort(function(a, b) {
return a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0;
});
return arr;
}