I am trying to sort an array with an array of strings in it. It is similar to this problem (Sort an array with arrays in it by string) but I wasn't sure how to implement that. My array is as follows
var myArray = [
['blala', 'alfred', '...'],
['jfkdj', 'berta', '...'],
['vkvkv', 'zimmermann', '...'],
['cdefe', 'albert', '...'],
];
I am trying to sort it alphabetically (not case sensitive) by the name or the second argument for the inner arrays. After that I want to sort by the first argument if there are two elements with the same second argument. I tried using the following but was unsuccessful and didn't really get why. Can anyone advise:
function Comparator(a,b){
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
}
var myArray = [
['blala', 'alfred', '...'],
['jfkdj', 'berta', '...'],
['vkvkv', 'zimmermann', '...'],
['cdefe', 'albert', '...'],
];
myArray = myArray.sort(Comparator);
To sort on the first argument after the second argument would I do this?
function Comparator(a,b){
if (a[1] < b[1]){
if (a[2] < b[2]) return -1
if (a[2] > b[2]) return 1;
}
return -1;
}
if (a[1] > b[1]) return 1;{
if (a[2] < b[2]) return -1
if (a[2] > b[2]) return 1;
}
return 1;
}
return 0;
}
var myArray = [
['blala', 'alfred', '...'],
['jfkdj', 'berta', '...'],
['vkvkv', 'zimmermann', '...'],
['cdefe', 'albert', '...'],
];
myArray = myArray.sort(Comparator);