w3resource
w3resource logo

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.


For more Practice: Solve these Related Problems:

  • Write a JavaScript function that sorts an array of numbers in ascending order using a custom comparator.
  • Write a JavaScript function that sorts an array without using the built-in sort() method.
  • Write a JavaScript function that sorts an array of objects by a specified property, ignoring case differences.
  • Write a JavaScript function that sorts an array and reverses the order when a flag parameter is set to true.

Go to:


PREV : Insert Dashes Between Evens.
NEXT : Most Frequent Array Item.

Improve this sample solution and post your code through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

(追記) (追記ここまで)


(追記) (追記ここまで)
(追記) (追記ここまで)


AltStyle によって変換されたページ (->オリジナル) /