By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Sort Array (task with advanced JavaScript functions)

+3 votes

Write a function that sorts an array with numeric values in ascending or descending order, depending on an argument that’s passed to it.

You will receive a numeric array and a string as arguments to the first function in your code. If the second argument is asc, the array should be sorted in ascending order (smallest values first). If it is desc, then the array is sorted in descending order (largest first).

The output should be the return value of your function and it is the sorted array.


Sample Input:

solve([14, 7, 17, 6, 8], 'asc');


[6, 7, 8, 14, 17]

Sample Input:

solve([14, 7, 17, 6, 8], 'desc');


[17, 14, 8, 7, 6]

asked in JavaScript category by user andrew

2 Answers

+3 votes

Here is my JavaScript code (solution):

function solve(arr, orderType) {
    if (orderType === "asc") {
        return arr.sort((a, b) => {
            return a - b;
    } else {
        return arr.sort((a, b) => {
            return b - a;

console.log(solve([14, 7, 17, 6, 8], 'desc'));


Arrays in JavaScript are by default sorted alphabetically, which means if we have an array of numbers [3, 1, 2, 10] and we call sort() on it, the result will be [1, 10, 2, 3]. You can however pass a sorting criteria as an argument in the form of a function. This function can be anonymously defined inline or a named function, or even a variable that holds a reference to a function.

Start by building the body of our main function, which takes two arguments and returns a sorted array, using the default sorting strategy.

If you test this function, you’ll see it uses the ASCII values to sort the objects inside the array. Next we need to pass an argument to the sort() function to get the desired result. It will consist of a special function which takes two arguments (current element and next element to be sorted), compares them and returns a value. If the value is zero, then they are equal. If it’s greater than zero, the first element is larger. If it’s less than zero, this means the second element is larger. In short, return a positive value to swap elements and zero or negative to keep the current order.

What we did is define an anonymous function directly in the argument space. Note we could have explicitly written a conditional statement, which returns 1 if a is greater than b. However, we know that if this is true, the result of the expression above will be positive, so we can use this much shorter version. If we want descending order, all we have to do is swap a and b in the expression.

Let’s use the functional nature of JavaScript and define the two comparator functions beforehand and assign them to variables.

We can now pass either of them to the sorting function, depending on what we need. We’ll save those in an object and use a string as a key, which would match the input shown in the problem description.

Now whenever we need a new sorting method, we can just define it as a separate function and add it to this object with its corresponding key. No further change will be necessary. Finally, we modify our initial call to sort() to receive one of the stored functions, depending on the second argument of our main function.

answered by user eiorgert
edited by user golearnweb
+1 vote