in Education by
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)

1 Answer

0 votes
by
One could give the numeric sorting option of String.prototype.localeCompare a try ... 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) { return a.name.localeCompare(b.name, undefined, { numeric: true }); } console.log(arr.sort(sorter)); .as-console-wrapper { min-height: 100%!important; top: 0; } Run code snippetExpand snippet

Related questions

0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    As it currently stands, this question is not a good fit for our Q&A format. We expect answers to ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I have function of "endgame" and here's how the function goes function endgame() { setScreen("scorescreen" ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I am working on a project where I need to use array values in the getElementById() in javascript. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I am attempting to solve a problem where if the elements in one array are squared, and all of the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    I'm trying to assemble and display image slices which have been preloaded into a two dimensional array, but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    Select the statement that has correct JavaScript syntax 1. document.write("text") 2. alert stop ; 3. console.log("text");...
asked Feb 23, 2021 by JackTerrance
0 votes
    I am getting Facebook users' friends birthday back from Facebook in the following format: 10/07/1967 or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I am getting Facebook users' friends birthday back from Facebook in the following format: 10/07/1967 or ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I have pain-time when making input that only allows float number with jquery library. my code can't ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    Write a program to store 10 names in an array. Arrange these in alphabetical orderby sorting. Print the sorted list. ... , LUCY, etc. Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    I've created a getSpectrum method using the getByteFrequencyData method on the Web Audio API's Analyser Node ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    is there a way to validate the pattern of an input by it's event object (without using a form)? ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 5, 2022 in Education by JackTerrance
...