Sorting Arrays In JavaScript
Arrays are used to store the data at contiguous index positions to enable random access to the data. Sorting the data stored in an array is required quite often. JavaScript provides sort() function to sort the arrays.for eaxmple,
arr=['orange','red','black','blue','pink'];
arr.sort();
for(item in arr)
{
document.write(arr[item]+'<br>');
}
The above array will be sorted in alphabetically sorted order.
Output:
black
blueorange
pink
red
arr=['orange','red','black','blue','pink'];
arr.sort();
arr.reverse();
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
red
pink
orange
blue
black
We can pass compare function to sort().
arr.sort();
arr.reverse();
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
red
pink
orange
blue
black
Sorting Array of Numbers
We can pass compare function to sort().
arr=[45,12,10,71,36,25,17];
arr.sort(function(a,b){
return a-b;
});
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
10
12
17
25
36
45
71
To sort in reverse order, we can just change compare function
arr=[45,12,10,71,36,25,17];
arr.sort(function(a,b){
return b-a;
});
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
71
45
36
25
17
12
10
arr.sort(function(a,b){
return a-b;
});
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
10
12
17
25
36
45
71
To sort in reverse order, we can just change compare function
arr=[45,12,10,71,36,25,17];
arr.sort(function(a,b){
return b-a;
});
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
71
45
36
25
17
12
10
Sorting in Random Order
To sort an array of numbers in random order we can use the random() function.
arr=[45,12,10,71,36,25,17];
arr.sort(function(a,b){
return 0.5-Math.random();
});
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
25
36
71
17
10
12
45
We can use Math.max for this task,
arr=[45,12,10,71,36,25,17];
function getMax(arr){
return Math.max.apply(null,arr)
};
document.write(getMax(arr));
Output:
71
To find the smallest or minimum number, use method Math.min
arr=[45,12,10,71,36,25,17];
function getMax(arr){
return Math.min.apply(null,arr)
};
document.write(getMax(arr));
Output:
71
mobiles=[{model:'Iphone',brand:'Apple'},
{model:'Pixel',brand:'Google'},
{model:'Oneplus7',brand:'OnePlus'},
{model:'Galaxy',brand:'Samsung'},
{model:'V15',brand:'Vivo'}];
mobiles.sort(function(a,b){
return a.brand-b.brand;
});
for(item in mobiles)
{
document.write(mobiles[item].brand+'<br>');
}
Output:
Apple
Google
OnePlus
Samsung
Vivo
Example
arr.sort(function(a,b){
return 0.5-Math.random();
});
for(item in arr)
{
document.write(arr[item]+'<br>');
}
Output:
25
36
71
17
10
12
45
Find the Largest and Smallest value in an Array
To find the largest and the smallest values of an array, we can sort the array in increasing order. The first element at index zero arr[0] will be the smallest element and the last element at index arr[arr.lenght-1] will be the largest, but this is an inefficient way to find the smallest and the largest and the smallest element of the array.We can use Math.max for this task,
arr=[45,12,10,71,36,25,17];
function getMax(arr){
return Math.max.apply(null,arr)
};
document.write(getMax(arr));
Output:
71
To find the smallest or minimum number, use method Math.min
arr=[45,12,10,71,36,25,17];
function getMax(arr){
return Math.min.apply(null,arr)
};
document.write(getMax(arr));
Output:
71
Sorting the Objects
We can sort the objects using our own compare function.mobiles=[{model:'Iphone',brand:'Apple'},
{model:'Pixel',brand:'Google'},
{model:'Oneplus7',brand:'OnePlus'},
{model:'Galaxy',brand:'Samsung'},
{model:'V15',brand:'Vivo'}];
mobiles.sort(function(a,b){
return a.brand-b.brand;
});
for(item in mobiles)
{
document.write(mobiles[item].brand+'<br>');
}
Output:
Apple
OnePlus
Samsung
Vivo
Example