Hi I want to sort the string based on numbers present in it e.g
I want to this in a manner
How do I achieve it in javascript?
You can use the Arrays
sort
method with a custom iterator function. e.g:
mylist = ["1.1.act2", "13.1.2.1.act3", "2.1.4.act56", "1.3.actorg", "3.1.3.args", "13.1.3.4.acr"];
mylist.sort(function(a, b) {
a = a.split("."); b = b.split(".");
var parts = Math.min(a.length, b.length);
for(var i = 0; i < parts; ++i) {
var numA = parseInt(a[i]); var numB = parseInt(b[i]);
if (numA != numB)
return numA > numB;
}
});
This is a stub and untested, but I guess you see where I'm heading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With