I want to sort the multiple elements in an array. The elements are like 123_test_site, 456_rit_kol, 65_aws_folder ....... output should be on the basis of 1st string starting character as 65_aws_folder,456_rit_kol,123_test_site. Can anyone help me.
I have a solution that might help you. My solution is like this:
const arr=['456_rit_kol', '65_aws_folder', '123_test_site',];
arr.sort((a, b) => {
let input1 = a.substring(a.indexOf('_'));
let input2 = b.substring(b.indexOf('_'));
if(input1 < input2){
return -1;
}else{
if(input1 < input2){
return 1;
}else{
return 0;
}
}
});
console.log(arr);