Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Angular Object Array Sorting

Trying to sort the array of object, the property of the object contains alphanumeric values, Tried methods which didn't worked

test.sort((a,b)=> a.Code.localeCompare(b.Code)); test.sort((a,b)=> +a.Code- +b.Code);

let test = [{Code: '16', Color: "Red"},
      {Code: '54', Color: "Yellow"},
      {Code: '24', Color: "Yellow"},
      {Code: '1600', Color: "Blue"},
      {Code: '16a', Color: "Green"},
      {Code: '22', Color: "Yellow"},
      {Code: '23', Color: "Yellow"}];

 test.sort((a,b) =>  a.Code.localeCompare(b.Code));
 console.log(test);

getting output as

[{"Code": "16", "Color": "Red" },
{"Code": "1600","Color": "Blue"},
{"Code": "16a","Color": "Green"},
{"Code": "22","Color": "Yellow"},
{"Code": "23","Color": "Yellow"},
{"Code": "24","Color": "Yellow"},
{"Code": "54","Color": "Yellow"}]

How I can get the Expected output as

[{"Code": "16", "Color": "Red" },    
{"Code": "22","Color": "Yellow"},
{"Code": "23","Color": "Yellow"},
{"Code": "24","Color": "Yellow"},
{"Code": "54","Color": "Yellow"},
{"Code": "1600","Color": "Blue"},
{"Code": "16a","Color": "Green"}]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could check for numbers and move number to top and sort by numbers or by string.

let test = [{ Code: '16', Color: "Red" }, { Code: '54', Color: "Yellow" }, { Code: '24', Color: "Yellow" }, { Code: '1600', Color: "Blue" }, { Code: '16a', Color: "Green" }, { Code: '22', Color: "Yellow" }, { Code: '23', Color: "Yellow" }];

test.sort(({ Code: a }, { Code: b }) =>
    isFinite(b) - isFinite(a) ||
    a - b ||
    a.localeCompare(b)
);
console.log(test);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

Try something as follows;

test.sort(a,b) => a.Code.localeCompare(b.Code, 'en', { numeric: true });

There is other possible solution if the above does not work as you expect.

Ref: Sort mixed alpha/numeric array

about 4 years ago · Juan Pablo Isaza Report

0

Thanks Nina a slight modification done to the solution as typescript was restricting typecasting

let test = [{ Code: '16', Color: "Red" }, { Code: '54', Color: "Yellow" }, { Code: '24', Color: "Yellow" }, { Code: '1600', Color: "Blue" }, { Code: '16a', Color: "Green" }, { Code: '22', Color: "Yellow" }, { Code: '23', Color: "Yellow" }];

test.sort(({ Code: a }, { Code: b }) =>
    +isFinite(+b) - +isFinite(+a) ||
    +a - +b ||
    a.localeCompare(b)
); 

console.log(test);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!