I am trying to sort an array of objects by the name field but the ordering is wrong.
The order I am getting is [1, 10, 11, 2, 20, 21, 3]
Instead of [1, 2, 3, .... 10, 11, 20, 21]
It is sorting but putting 10 ahead of 2
Here is the code I am currently using.
const arr = [
{
"name": "Action 10",
"color": "transparent",
"type": "components"
},
{
"name": "Action 11",
"color": "transparent",
"type": "components"
},
{
"name": "Action 2",
"color": "transparent",
"type": "components"
},
{
"name": "Action 20",
"color": "transparent",
"type": "components"
},
{
"name": "Action 21",
"color": "transparent",
"type": "components"
},
{
"name": "Action 3",
"color": "transparent",
"type": "components"
},
{
"name": "Action 4",
"color": "transparent",
"type": "components"
},
{
"name": "Action 5",
"color": "transparent",
"type": "components"
},
{
"name": "Action 6",
"color": "transparent",
"type": "components"
},
{
"name": "Action 1",
"color": "transparent",
"type": "components"
}
]
function sorter(a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
}
console.log(arr.sort(sorter));
Run code snippetExpand snippet
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)