JavaScript - Sort JS Array with Different Data Types
Last Updated :
15 Nov, 2024
To sort a JS array having different data types we can sort the elements by converting them to similar types, using type-based sorting, or by using external libraries like lodash and underscore.js.
1. Sorting with String Conversion
Convert the elements to strings using String() constructor, and then use JavaScript array.sort() method.
JavaScript
const arr = [30, "JS", true, 10, "CSS", false, 20];
// Convert to strings
const arr1 = arr.map(item => String(item));
arr1.sort();
console.log(arr1);
Output[
'10', '20',
'30', 'CSS',
'JS', 'false',
'true'
]
2. Type Based Sorting
Use custom comparision funciton to sort on the basis of elemen types.
JavaScript
const arr = [30, "JS", true, 10, "CSS", false, 20];
// Write a custom comparison function
function myFunction(a, b) {
const typeA = typeof a;
const typeB = typeof b;
if (typeA === typeB) {
// If same type, compare values directly
return a > b ? 1 : -1;
} else {
// If different types, sort by data type
return typeA > typeB ? 1 : -1;
}
}
arr.sort(myFunction);
console.log(arr);
Output[
false, true, 10,
20, 30, 'CSS',
'JS'
]
we can also set custom priority order for different types.
JavaScript
const arr = [30, "JS", true, 10, "CSS", false, 20];
// Write a custom comparison function
function myFunction(a, b) {
const typeOrder = ['number', 'string', 'object',
'boolean', 'undefined', 'symbol'];
const typeA = typeof a;
const typeB = typeof b;
if (typeA === typeB) {
// If same type, compare values directly
return a > b ? 1 : -1;
} else {
// If different types, sort by data type
return typeOrder.indexOf(typeA) > typeOrder.indexOf(typeB) ? 1 : -1;
}
}
arr.sort(myFunction);
console.log(arr);
Output[
10, 20, 30,
'CSS', 'JS', false,
true
]
3. Using Lodash.sortBy() Method
Pass the typeof parameter in _.sortBy() method for type based sorting.
JavaScript
const _ = require('lodash');
const arr = [30, "JS", true, 10, "CSS", false, 20];
const arr1 = _.sortBy(arr, [(item) => typeof item]);
console.log(arr1);
Output
[
true, false, 30,
10, 20, 'JS',
'CSS'
]
Similar Reads
How to Sort JSON Array in JavaScript by Value ? Sorting is the process of arranging elements in a specific order. In JavaScript, you can sort a JSON array by value using various methods as listed and discussed below with their practical implementation. Table of Content Using sort() FunctionUsing Array.prototype.reduce()Using Bubble SortUsing sort
3 min read
Sort An Array Of Arrays In JavaScript The following approaches can be used to sort array of arrays in JavaScript.1. Using array.sort() Method- Mostly UsedJS array.sort() is used to sort and update the original array in ascending order.JavaScriptlet arr = [[3, 2], [1, 4], [2, 5], [2, 0]]; arr.sort(); console.log(arr);Output[ [ 1, 4 ], [
2 min read
JavaScript - Sort an Array in Reverse Order in JS JS array.sort() Method sorts the array in ascending order. We can make it descending or reverse order using array.reverse() method. Below are the following ways to sort an array in reverse order:1. Using array.sort() with array.reverse()First, sort the given array of strings using JS array.sort() me
2 min read
Is JavaScript Array Sort Stable ? Javascript Array Sort is stable: As of version 10 (or ECMAScript 2019), the specification requires Array.prototype.sort to be stable. In javascript, stable sorting is if an array objects with (key, value) pair has equal keys appear in the same order in sorted output as they appear in the input data
3 min read
JavaScript - How to Sort Rows in a Table? Rows in a table are horizontal sections that hold data, organizing information across columns in a structured, easy-to-read format. To sort table rows in JavaScript, create a function that compares rows based on cell values. Use a loop to check and swap rows as needed until all rows are in order.App
4 min read