Unable to sort array of objects by version number in JavaScript

Kiran

I have an array of objects in that I want to sort the array based on the xxx version number string. But it seems there is an issue in sorting the order as per my desire.

I have seen the existing solutions for example, the Existing Question Suggested by community. But it dealt with plain arrays instead of array of objects. I can tweak the answer as per needs. But the solutions seems to be pretty long. So I'm looking for a much simpler solutions.

Example Object:

my_list = [{"xxx": "9.6", "no_of_occurrence": 1 }, {"xxx": "9.2", "no_of_occurrence": 1},
{"xxx": "9.11", "no_of_occurrence": 1}, {"xxx": "9.10", "no_of_occurrence": 1}]

After sorting My desired output should be

Either

[{"xxx": "9.11", "no_of_occurrence": 1 }, {"xxx": "9.10", "no_of_occurrence": 1},
{"xxx": "9.6", "no_of_occurrence": 1}, {"xxx": "9.2", "no_of_occurrence": 1}]

or

[{"xxx": "9.2", "no_of_occurrence": 1 }, {"xxx": "9.6", "no_of_occurrence": 1},
{"xxx": "9.10", "no_of_occurrence": 1}, {"xxx": "9.11", "no_of_occurrence": 1}]

To achieve this I'm facing some issues. The decimal places are not treated the way they should be.

What I've tried so far is:

Approach 1:

my_list.sort((a, b) => 
     {
       if (a['xxx'] < b['xxx']) {
         return -1;
       }
       if (a['xxx'] > b['xxx']) {
         return 1;
       }
       return 0;
     }
  );

Approach 2:

my_list.sort((a, b) => {a['xxx'] - b['xxx']});

I did tried converting the string to float by using the "parseFloat(a['xxx'])" and "parseFloat(a['xxx']).toFixed(2)" as well. But nothing seems to be working.

The output generated by the above sort functions is:

[{
 no_of_occurrence: 1,
 xxx: "9.6"
}, {
 no_of_occurrence: 1,
 xxx: "9.2"
}, {
 no_of_occurrence: 1,
 xxx: "9.11"
}, {
 no_of_occurrence: 1,
 xxx: "9.10"
}]

However I tried, I'm not able to achieve what I desired. How can I treat the 9.10 is greater than 9.2? I know javascript treats 9.10 as 9.1 vise versa. But the data I received from the client and the client expectation is 9.10 is > 9.2.

Your help is appreciated.

Carsten Massmann

Or simply do this:

const my_list = [{"xxx": "9.6", "no_of_occurrence": 1 }, {"xxx": "9.2", "no_of_occurrence": 1},
{"xxx": "9.11", "no_of_occurrence": 1}, {"xxx": "9.10", "no_of_occurrence": 1}];

console.log(my_list.sort((a,b)=>{a=a.xxx.split(".");b=b.xxx.split(".");
 return a[0]-b[0] || a[1]-b[1];}));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Javascript Array of Objects Sort

Javascript sort objects in array by first column smallest number to largest

Sort array of objects of objects in javascript

Javascript sort array of objects on key

How to sort the array of objects in javascript?

Javascript sort an objects by another array

Sort array of objects with another in javascript

Sort an array of objects dynamically javascript

Sort by multiple objects in a array in Javascript

Sort array of objects in javascript with two sort conditions

Unable to sort array according to containing number

Sort version-dotted number strings in Javascript?

Javascript sort array of objects using array of priority

JavaScript sort an array of objects based array of properties

Reordering version dotted serial number array of objects

Can I use .sort() to sort an array of objects based on a number property?

Javascript: Sort an array of objects based on another array of objects

How to sort Javascript objects in an array using regex

How to sort an array of objects then map through it in JavaScript?

Sort array of objects following multiple rules in javascript

Javascript - Sort array of objects by date then by time

How to sort a nested array of objects in javascript?

Sort array of JavaScript objects by property value

Javascript sort array of objects by a boolean property

How to sort an array of objects with jquery or javascript

Sort array of objects by arbitrary list in JavaScript

Javascript - Sort array of objects by property date

Javascript sort algorithm for linked sections objects array

Sort array of objects, then group by id (JavaScript)