- 5 years ago
- Zaid Bin Khalid
- 3,281 Views
-
6
In this session, you will learn how to sort arrays elements in JavaScript with the help of an example.
Sorting an Array
When you are working with arrays the most critical part is sorting them. For example, sorting is used to show the city or country names in alphabetical order
For sorting array elements in alphabetical order, The JavaScript Array has a built-in method sort( ). This method can be shown in the stated example:
var fruits = ["Banana", "Orange", "Apple", "Papaya", "Mango"];
var sorted = fruits.sort();
alert(fruits); // Outputs: Apple,Banana,Mango,Orange,Papaya
alert(sorted); // Outputs: Apple,Banana,Mango,Orange,Papaya
Reversing an Array
To reverse the order of the elements of an array, you can use the reverse( ) method. This method will help to change an array in such a way that the first array become last and the last array element becomes the first. As shown in the example.
var counts = ["one", "two", "three", "four", "five"];
var reversed = counts.reverse();
alert(counts); // Outputs: five,four,three,two,one
alert(reversed); // Output: five,four,three,two,one
Sorting Numeric Arrays
With the help of sort( ) method, it will show a unique result, when sort method applied on numeric arrays. This array method contains numeric values. As shown in the example.
var numbers = [5, 20, 10, 75, 50, 100];
numbers.sort(); // Sorts numbers array
alert(numbers); // Outputs: 10,100,20,5,50,75
Now you can see the output result is changed from what we have thought. This kind of thing happens because of the sort( ) method. It sorts the numeric array elements by converting it into strings. To overcome this problem with the digital array, you can use compare function as shown in the example.
var numbers = [5, 20, 10, 75, 50, 100];
// Sorting an array using compare function
numbers.sort(function(a, b) {
return a - b;
});
alert(numbers); // Outputs: 5,10,20,50,75,100
The Minimum and Maximum Value Finding in an Array
To find the maximum and minimum value inside an array you can use the apply( ) method in the form of combination with the Math.max( ) and Math.min( ), as shown in the example.
var numbers = [3, -7, 10, 8, 15, 2];
// Defining function to find maximum value
function findMax(array) {
return Math.max.apply(null, array);
}
// Defining function to find minimum value
function findMin(array) {
return Math.min.apply(null, array);
}
alert(findMax(numbers)); // Outputs: 15
alert(findMin(numbers)); // Outputs: -7
Sorting an Array of Objects
To sort an array of objects, the sort( ) method is used in the compare function.
As shown in the example, you can see how to sort an array of objects by property values.
var persons = [
{ name: "Harry", age: 14 },
{ name: "Ethan", age: 30 },
{ name: "Peter", age: 21 },
{ name: "Clark", age: 42 },
{ name: "Alice", age: 16 }
];
// Sort by age
persons.sort(function (a, b) {
return a.age - b.age;
});
console.log(persons);
// Sort by name
persons.sort(function(a, b) {
var x = a.name.toLowerCase(); // ignore upper and lowercase
var y = b.name.toLowerCase(); // ignore upper and lowercase
if(x < y) {
return -1;
}
if(x > y) {
return 1;
}
// names must be equal
return 0;
});
console.log(persons);
- 5 years ago
- Zaid Bin Khalid
- 3,281 Views
-
6