• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Sort an JSON Object Array based on alphnumeric key attribute in JavaScript

Resolved: Sort an JSON Object Array based on alphnumeric key attribute in JavaScript

0
By Isaac Tonny on 04/04/2023 Issue
Share
Facebook Twitter LinkedIn

In this post, we will see how to resolve Sort an JSON Object Array based on alphnumeric key attribute in JavaScript

Question:

I want to sort this JSON array based on unitId
I want an output array like this

Best Answer:

To sort the given array based on the alphanumeric unitId, you can use the sort() method with a custom comparator function. Here’s an example:
This will output the desired sorted array:
The sort() function takes a custom comparator that compares two elements a and b. The unitId values are first stripped of the ‘unit’ string and then converted to integers using parseInt(). The difference between the unitId integers is returned to determine the sort order.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

arraylist arrays javascript json sorting
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: How to stop a thread that has a blocking function from easygui in python

05/04/2023

Resolved: Removing null keys from a json array of objects

05/04/2023

Resolved: How can I generate at compile-time a separate OpenAPI Swagger.json file for each Controller in my ASP.NET project?

05/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.