Q:

sort float numbers as a natural numbers

I have a comma separated float numbers.
 var example = "1.1, 1.10, 1.2, 3.1, 3.14, 3.5";

and I want to sort this float numbers like,
"1.1, 1.2, 1.10, 3.1, 3.5, 3.14"

actually in my case, the numbers which are after decimals will consider as a natural numbers, so 1.2 will consider as '2' and 1.10 will consider as '10' thats why 1.2 will come first than 1.10.
and suggestion or example would be great for me, thanks.
Actually I want to first sort the array on the basis of numbers which are before decimals :) then the above logic will run.

A:

You can use .sort with custom compare function, like so

var example = "1.1, 1.10, 1.2, 3.1, 3.14, 3.5";

var res = example.split(',').sort(function (a, b) {
  var result;
  
  a = a.split('.'), 
  b = b.split('.');

  while (a.length) {
    result = a.shift() - (b.shift() || 0);
    
    if (result) {
      return result;
    }
  }

  return -b.length;
}).join(',');

console.log(res);

