w3resource

JavaScript: Sort the items of an array

JavaScript Array: Exercise-7 with Solution

Sort Array

Write a JavaScript program to sort the items of an array.

Sample array : var arr1 = [ -3, 8, 7, 6, 5, -4, 3, 2, 1 ];
Sample Output : -4,-3,1,2,3,5,6,7,8

Visual Presentation:

JavaScript: Sort the items of an array

Sample Solution:

JavaScript Code:

// Declare and initialize the original array
var arr1 = [-3, 8, 7, 6, 5, -4, 3, 2, 1];

// Declare an empty array to store the sorted result
var arr2 = [];

// Initialize variables to track the minimum, maximum, and position in the original array
var min = arr1[0];
var pos;
var max = arr1[0];

// Find the maximum value in the original array
for (i = 0; i < arr1.length; i++) {
    if (max < arr1[i]) max = arr1[i];
}

// Selection sort algorithm: Iterate over the original array
for (var i = 0; i < arr1.length; i++) {
    // Iterate over the original array to find the minimum element
    for (var j = 0; j < arr1.length; j++) {
        // Check if the element is not marked as processed (not equal to "x")
        if (arr1[j] != "x") {
            // Find the minimum element and its position in the original array
            if (min > arr1[j]) {
                min = arr1[j];
                pos = j;
            }
        }
    }
    // Store the minimum element in the sorted array
    arr2[i] = min;

    // Mark the minimum element as processed by replacing it with "x" in the original array
    arr1[pos] = "x";

    // Reset min to the maximum value for the next iteration
    min = max;
}

// Output the sorted array
console.log(arr2);

Output:

[-4,-3,1,2,3,5,6,7,8]

Flowchart:

Flowchart: JavaScript: Display the colors entered in an array by a specific format

ES6 Version:

// Declare and initialize the original array
const arr1 = [-3, 8, 7, 6, 5, -4, 3, 2, 1];

// Declare an empty array to store the sorted result
const arr2 = [];

// Initialize variables to track the minimum, maximum, and position in the original array
let min = arr1[0];
let pos;
let max = arr1[0];

// Find the maximum value in the original array
for (let i = 0; i < arr1.length; i++) {
    if (max < arr1[i]) max = arr1[i];
}

// Selection sort algorithm: Iterate over the original array
for (let i = 0; i < arr1.length; i++) {
    // Iterate over the original array to find the minimum element
    for (let j = 0; j < arr1.length; j++) {
        // Check if the element is not marked as processed (not equal to "x")
        if (arr1[j] != "x") {
            // Find the minimum element and its position in the original array
            if (min > arr1[j]) {
                min = arr1[j];
                pos = j;
            }
        }
    }
    // Store the minimum element in the sorted array
    arr2[i] = min;

    // Mark the minimum element as processed by replacing it with "x" in the original array
    arr1[pos] = "x";

    // Reset min to the maximum value for the next iteration
    min = max;
}

// Output the sorted array
console.log(arr2);

Live Demo:

See the Pen JavaScript - Sort the items of an array- array-ex- 7 by w3resource (@w3resource) on CodePen.


Improve this sample solution and post your code through Disqus.

Previous: Write a JavaScript program which accept a number as input and insert dashes (-) between each two even numbers.
Next: Write a JavaScript program to find the most frequent item of an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-array-exercise-7.php