Sort array by decimal then by string - Javascript -
i need sort array of format, using plain old javascript.
var arr = ["0.01 basic", "0.01 sef", "0.015 basic"];
what need array sorted first decimal string , produce output below;
arr = ["0.01 basic", "0.015 basic", "0.01 sef"];
i cannot use jquery in performing sort. plain old javascript.
you can :
arr.sort(function(a,b){ var @ = a.split(' '), bt = b.split(' '); if (at[1]!=bt[1]) return at[1]>bt[1] ? 1 : -1; return parsefloat(at[0])-parsefloat(bt[0]); });
if want sort big array, might faster cache keys. doesn't matter arrays.
example :
["0.01 basic", "0.01 sef", "0.015 basic", "0.2 basic", "0.001 sef", "0.2 aaa"] -> ["0.2 aaa", "0.01 basic", "0.015 basic", "0.2 basic", "0.001 sef", "0.01 sef"]
Comments
Post a Comment