1

Actually we are using d3 with grails. there is scenario like we need to sort the given input according to integer, float and string.

I can check weather the input is number or string with `

if(typeof(d["colorField"])=='number'){
  L_KEY_NUMERIC = true;
}else{
   L_KEY_NUMERIC = true;
}

but this is failing for double value. double values are not getting sorted properly.

So, i need to check whether the key is double or integer using jquery. currently I'am using sort() method which sorts for integer normally but not for double values. once i detect the key is double or float which sort method i need to use.

And how to sort float key via ascending order in jquery.

Any help appreciated.

2 Answers2

0

You should use d["colorField"] % 1 === 0 to check if key is number:

if(d["colorField"] % 1 === 0){
 //if number is integer 
 L_KEY_NUMERIC = true;
}else{
 //if number is float
 L_KEY_NUMERIC = true;
}
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

Or you could use parseFloat(string)

e.g.
if (!isNan(parseFloat(string))){
  //your code here
}
Craig Poole
  • 740
  • 8
  • 19