djfkdjfkd39939
djfkdjfkd39939

Reputation: 393

Sorting Array of Arrays with Strings in it

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);

Upvotes: 4

Views: 691

Answers (1)

Ram
Ram

Reputation: 144689

You could code:

function Comparator(a, b) {
    // you can use the `String.prototype.toLowerCase()` method
    // if the comparison should be case insensitive
    if (a[1] < b[1]) return -1;
    if (a[1] > b[1]) return 1;
    if (a[0] < b[0]) return -1;
    if (a[0] > b[0]) return 1;
    return 0;
}

The above function at first sorts the elements based on the second element of the arrays. If the second elements are equal then it sorts them based on the first elements and if a[1] === b[1] and a[0] === b[0] it returns 0 which leaves the a and b positions unchanged.

From MDN Array.prototype.sort documentation:

If compareFunction is supplied, the array elements are sorted according to the return value of the compare function. If a and b are two elements being compared, then:

  • If compareFunction(a, b) is less than 0, sort a to a lower index than b, i.e. a comes first.
  • If compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee this behaviour, and thus not all browsers (e.g. Mozilla versions dating back to at least 2003) respect this.
  • If compareFunction(a, b) is greater than 0, sort b to a lower index than a. compareFunction(a, b) must always return the same value when given a specific pair of elements a and b as its two arguments. If inconsistent results are returned then the sort order is undefined.

Upvotes: 3

Related Questions