Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have an array as follows

var arry = [ [1, "Convention Hall", "Mumbai", 10, "XYZ Company"], 
             [2, "Auditorium", "Delhi", 10, "ABC Company"], 
             [3, "CenterHall", "Bangalore", 10, "ZZZ Company"],
            ....
            ]

I want to sort the array alphabetically based on the third item of array i.e arry[n][2]

How to do this.

share|improve this question
    
So, its already sorted in this case? – thefourtheye Jan 2 '14 at 12:10
    
arry.sort(function (a, b) { return a[2] - b[2]; }); – Givi Jan 2 '14 at 12:12
    
It's already sorted in this case, but check my answer. I changed city names. – Ali Gajani Jan 2 '14 at 12:18
up vote 2 down vote accepted

http://jsfiddle.net/VPrAr/

enter image description here

You can use the arry.sort(). The default is alphanumeric and ascending.

So it would be:

var arry = [ [1, "Convention Hall", "Dangalore", 10, "XYZ Company"], 
             [2, "Auditorium", "Belhi", 10, "ABC Company"], 
             [3, "CenterHall", "Aumbai", 10, "ZZZ Company"],
            ];
var x =arry.sort(function(a,b){ return a[2] > b[2] ? 1 : -1; });
alert(x);
share|improve this answer
    
Since sort is mutator method there's no need to assign it to new variable, and what about a[2] === b[2]? – Givi Jan 2 '14 at 12:21
    
I agree. The question poster gets the point though :) – Ali Gajani Jan 2 '14 at 12:21
    
Its working fine – n92 Jan 2 '14 at 12:22
    
Thanks Vinay, don't forget to +1 (up-vote) too :) – Ali Gajani Jan 2 '14 at 12:23

Array.prototype.sort function expects a function as a parameter, which accepts two parameters and returns any of -1, 0 or 1.

I am a very big fan of functional programming, so I came up with this. This offers flexibility.

  1. You can sort based on any element
  2. You can even reverse the order of sorting
  3. You can customize, how the objects have to be compared.

function basicComparator(first, second) {
    if (first === second) {
        return 0;
    } else if (first < second) {
        return -1;
    } else {
        return 1;
    }
}

function compareNthElements(n, comparatorFunction, reverse) {
    return function(first, second) {
        if (reverse === true) {
            return comparatorFunction(second[n], first[n]);
        } else {
            return comparatorFunction(first[n], second[n]);
        }
    }
}

Thats it. Now, call the sort function like this

arry.sort(compareNthElements(1, basicComparator, true));  // Sorts first field and in reverse
arry.sort(compareNthElements(2, basicComparator));        // Sorts second field
share|improve this answer
    
Why not simply return first - second;? – Givi Jan 2 '14 at 12:32
    
@Givi It may not work properly for all data types. For example, if both the elements being compared are objects, it will return NaN. So, I made the function more flexible, to pass the comparator function as well :) – thefourtheye Jan 2 '14 at 12:44

Use a sort function for example:

arry.sort(function(a,b){
   return a[2] > b[2] ? 1 : -1;
});
share|improve this answer

try this

//WITH FIRST COLUMN
     arry = arry.sort(function(a,b) {
      return a[0] > b[0];
    });


//WITH SECOND COLUMN
arry = arry.sort(function(a,b) {
 return a[1] > b[1];
 });


//WITH THIRD COLUMN
//and you want this code below
arry = arry.sort(function(a,b) {
 return a[2] > b[2];
 });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.