--description--
The sort
method sorts the elements of an array according to the callback function.
For example:
function ascendingOrder(arr) {
return arr.sort(function(a, b) {
return a - b;
});
}
ascendingOrder([1, 5, 2, 3, 4]);
This would return the value [1, 2, 3, 4, 5]
.
function reverseAlpha(arr) {
return arr.sort(function(a, b) {
return a === b ? 0 : a < b ? 1 : -1;
});
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
This would return the value ['z', 's', 'l', 'h', 'b']
.
JavaScript's default sorting method is by string Unicode point value, which may return unexpected results. Therefore, it is encouraged to provide a callback function to specify how to sort the array items. When such a callback function, normally called compareFunction
, is supplied, the array elements are sorted according to the return value of the compareFunction
: If compareFunction(a,b)
returns a value less than 0 for two elements a
and b
, then a
will come before b
. If compareFunction(a,b)
returns a value greater than 0 for two elements a
and b
, then b
will come before a
. If compareFunction(a,b)
returns a value equal to 0 for two elements a
and b
, then a
and b
will remain unchanged.
--instructions--
Use the sort
method in the alphabeticalOrder
function to sort the elements of arr
in alphabetical order. The function should return the sorted array.
--hints--
Your code should use the sort
method.
assert(__helpers.removeJSComments(code).match(/\.sort/g));
alphabeticalOrder(["a", "d", "c", "a", "z", "g"])
should return ["a", "a", "c", "d", "g", "z"]
.
assert(
JSON.stringify(alphabeticalOrder(['a', 'd', 'c', 'a', 'z', 'g'])) ===
JSON.stringify(['a', 'a', 'c', 'd', 'g', 'z'])
);
alphabeticalOrder(["x", "h", "a", "m", "n", "m"])
should return ["a", "h", "m", "m", "n", "x"]
.
assert(
JSON.stringify(alphabeticalOrder(['x', 'h', 'a', 'm', 'n', 'm'])) ===
JSON.stringify(['a', 'h', 'm', 'm', 'n', 'x'])
);
alphabeticalOrder(["a", "a", "a", "a", "x", "t"])
should return ["a", "a", "a", "a", "t", "x"]
.
assert(
JSON.stringify(alphabeticalOrder(['a', 'a', 'a', 'a', 'x', 't'])) ===
JSON.stringify(['a', 'a', 'a', 'a', 't', 'x'])
);
--seed--
--seed-contents--
function alphabeticalOrder(arr) {
// Only change code below this line
return arr
// Only change code above this line
}
alphabeticalOrder(["a", "d", "c", "a", "z", "g"]);
--solutions--
function alphabeticalOrder(arr) {
return arr.sort();
}