I have some code that sorts names it is given alphabetically.
The problem I am having is with the way it handles decimals.
It orders the names like below (i would rather it increment numerically):
DOG - 1.0510
DOG - 1.1031
DOG - 11.1792
DOG - 12.0920
DOG - 12.1170
DOG - 2.0186 <-- should be after "DOG - 12.117" ???
DOG - 21.4070
DOG - 22.0790
DOG - 23.0390
CAT - 1.0810
CAT - 1.1071
CAT - 11.1592
CAT - 12.0691
CAT - 12.1718
CAT - 2.0186 <-- again should be after "CAT -12.1718" ???
CAT - 21.1403
CAT - 22.081
CAT - 23.069
I have the name/values inside an array of objects like below:
var array = [
{
"myname":"DOG",
"value":1.0051
},
{
"myname":"DOG",
"value":1.1071
}
];
Here is the code I am using from a snippet I found online.
function(x, y){
var xName=x.myname.toLowerCase(), yName=y.myname.toLowerCase()
if (xName < yName) //string sort ascending
return -1
if (xName < yName)
return 1
return 0 //return default value (without sorting)
}
Think the problem is that you need to sort by the value as well but you are only sorting by the name.
The below should fix it, it sorts alphabetically and then numerically.
Code: