0

If i have an array:

myArray = [['0','Mouse'],['1','Dog'],['2','Cat'],['3','Gerbil']];

How can I alphabetize the array based on the, in this case, animals?

myArray = alpha(myArray);

Results:

myArray = [['2','Cat'],['1','Dog'],['3','Gerbil'],['0','Mouse']];
Damien
  • 4,093
  • 9
  • 39
  • 52

2 Answers2

2

you can use sort function

var myArray = [['0','Mouse'],['1','Dog'],['2','Cat'],['3','Gerbil']];
console.log(alpha(myArray));

var arr2 = [['5','Mouse'],['0','Mouse'],['1','Dog'],['2','Cat'],['3','G‌​erbil']];
console.log('another array', alpha(arr2));

function alpha(arr) {
  return arr.sort((a, b) => a[1] > b[1]);
}
Andriy
  • 14,781
  • 4
  • 46
  • 50
  • sort works in place, your return suggest, you get a new array. – Nina Scholz Jan 05 '17 at 19:34
  • I am sorry @NinaScholz, did not understand your point, please explain.. – Andriy Jan 05 '17 at 19:39
  • your sort does not work with for example: `var myArray = [['5','Mouse'],['0','Mouse'],['1','Dog'],['2','Cat'],['3','Gerbil']];`, because of the wrong return value of the sort function. – Nina Scholz Jan 05 '17 at 19:54
  • I added mentioned by you array to my answer and see no problem there, oh, you probably mean non-latin, non-ascii chars..., I think it will work, but should be tested.... – Andriy Jan 05 '17 at 20:07
  • you may have a look to my comment here: http://stackoverflow.com/a/41493346/1447675 – Nina Scholz Jan 05 '17 at 20:09
  • ok, thanks @Nina for pointing to this – Andriy Jan 05 '17 at 20:11
  • Tried the sort and it works for the most part, but for some reason the first item on my list of 90 or so items starts with a "W". Somethings off :( – Damien Jan 05 '17 at 20:16
  • Try String#localeCompare proposed by @NinaScholz – Andriy Jan 05 '17 at 20:48
  • just to add my liked commet (of the now deleted answer): it depends on the implementation of sort by the vendor. your callback returns only two values instead of three, because less than zero values are missing. this leads to fancy values in certain user agents, like edge. you could minimize this behaviour if you take the whole possible spectrum of return values of less than zero, zero or greater than zero values. – Nina Scholz Jan 05 '17 at 21:00
1

You could use Array#sort

The sort() method sorts the elements of an array in place and returns the array. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

in combination with String#localeCompare

The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

var array = [['5', 'Mouse'], ['0', 'Mouse'], ['1', 'Dog'], ['2', 'Cat'], ['3', 'Gerbil']];

array.sort(function (a, b) {
    return a[1].localeCompare(b[1]);
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • How does it know to sort on the 'Mouse','Dog' index? Also, what would this look like on a one-dimensional array? – Damien Jan 05 '17 at 20:22
  • do you see `[1]`? it uses the index for the inner arrays. – Nina Scholz Jan 05 '17 at 20:24
  • i see the [1], but why isn't it something like [i][1] since it's a multi-dimension array? – Damien Jan 05 '17 at 20:25
  • the sort method uses the items of the array and checks two elements and then the function takes fom an element the item with index `1` and from the other item the other value with index `1` and returns a value from `localCompare`. please have a look to the first given link in my answer. – Nina Scholz Jan 05 '17 at 20:37
  • so if it was a single dimension array, would it be the same exact code? – Damien Jan 06 '17 at 14:54
  • yes, without using the index. – Nina Scholz Jan 06 '17 at 19:11