paragraph-numbers-sort

node v8.9.4
version: 2.0.0
endpointsharetweet
//working comparer function to be passed to Array.sort() //test array var arr = ["9","2.10", "66.9.90008.56.5", "1.1", "1.2", "1.12", "2.10.1", "2.10.4", "0", "2.100.5", "2.100.1", "3.5", "3.101.2.3.4", "5.6", "0.3", "5.202", "1.0.122", "2.100"]; paragraphNumbersSort = function (a, b){ var a1 = a.split('.'); var b1 = b.split('.'); var diff = a1.length - b1.length; if (diff < 0) for(var i = diff; i < 0; i++) a1.push("0"); else for(var i = 0; i < diff; i++) b1.push("0"); var res = 0; a1.some(function(n, i){ res = n - b1[i]; return (res != 0 ); }); return res; } //passing the comparer. Expected behaviour 1.2 must be before 1.12 and etc. arr.sort(paragraphNumbersSort);
Loading…

no comments

    sign in to comment