I have following array:-
const a=["5.1.1","5.1.12","5.1.2"]
I want to sort the above to get the following:-
5.1.1,5.1.2,5.1.12
But if I call normal sort i get following.
5.1.1,5.1.12,5.1.2
I tried implementing this using custom sort. Following is my code:-
let data = a.sort()
data.sort(function(x,y){
const arrX = x.split(".")
const arrY = y.split('.')
if(parseInt(arrY[1]) > parseInt(arrX[1])){
return -1
}
if(parseInt(arrX[2]) > parseInt(arrY[2])){
return 1
}
})
return data
It works fine, but feels bit hacky. Does someone have a better solution for it?