Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array like this

[
     ["Adrian Garcia", "42"],
     ["Alberto Guglielmi", "42"],
     ["Alice Fung Yee Ng", "15"],
     ["Christopher Wilson", "4"],
     ["Claudio Meneghetti", "13"],
     ["Bahadir Tanriover", "15"],
     ["Baitinger Martin", "36"],
     ["Bill Cash", "15"],
     ["Brian Kuhlmann", "15"],
     ["Caesar Lima", "15"],
     ["Carl Tremblay", "42"],
     ["Aorta", "42"],
     ["Charles Harris", "15"],
     ["Chet Morrison", "15"],
     ["Chico Audi", "11"],
     ["Andreas Gemperle", "42"],
     ["Angel Burns", "42"],
     ["Arno Bosma", "42"],
     ["Chris Bailey", "15"],
     ["Chris Close", "1"],
     ["Christoph Martin Schmid", "42"],
     ["Ben Isselstein", "25"],
     ["Colin Thomas", "15"],
     ["Craig Cameron Olsen", "42"],
     ["Craig Easton", "42"]
]

I need to sort this array by numbers and also the name in ascending order. I find most of the solutions for sort by single value. But I need to sort by two values.

I need output like below.

[
    ["Chris Close", "1"]
    ["Christopher Wilson", "4"]
    ["Chico Audi", "11"]
    ["Claudio Meneghetti", "13"]
    ["Alice Fung Yee Ng", "15"]
    ["Bahadir Tanriover", "15"]
    ["Bill Cash", "15"]
    ["Brian Kuhlmann", "15"]
    ["Caesar Lima", "15"]
    ["Charles Harris", "15"]
    ["Chet Morrison", "15"]
    ["Chris Bailey", "15"]
    ["Colin Thomas", "15"]
    ["Ben Isselstein", "25"]
    ["Baitinger Martin", "36"]
    ["Adrian Garcia", "42"]
    ["Alberto Guglielmi", "42"]
    ["Carl Tremblay", "42"]
    ["Aorta", "42"]
    ["Andreas Gemperle", "42"]
    ["Angel Burns", "42"]
    ["Arno Bosma", "42"]
    ["Christoph Martin Schmid", "42"]
    ["Craig Cameron Olsen", "42"]
    ["Craig Easton", "42"]
]
share|improve this question
1  
I tried most of the solutions -- Show us what you tried. –  George Aug 13 at 9:50
    
All that solutions are sort with only single value. Ex: myArray.sort(function(a, b) { return a[1] - b[1]; }); –  RajivRisi Aug 13 at 9:53
    
@RajivRisi so you can use not single value in sort function :-) –  Grundy Aug 13 at 9:53
1  
OP, this has nothing to do with jQuery. Just plain JavaScript. –  putvande Aug 13 at 9:57
1  
Can you show what the output should look like? –  putvande Aug 13 at 10:09

3 Answers 3

up vote 1 down vote accepted

A shorter version would be just to use JavaScripts own sort function:

// First sort on number (second column)
array.sort(function(a, b) { return a[1] - b[1] });
// Then sort on name (first column)
array.sort(function(a, b) { return a[0] - b[0] });

which outputs as

[
    ["Chris Close", "1"],
    ["Christopher Wilson", "4"],
    ["Chico Audi", "11"],
    ["Claudio Meneghetti", "13"],
    ["Alice Fung Yee Ng", "15"],
    ["Bahadir Tanriover", "15"],
    ["Bill Cash", "15"],
    ["Brian Kuhlmann", "15"],
    ["Caesar Lima", "15"],
    ["Charles Harris", "15"],
    ["Chet Morrison", "15"],
    ["Chris Bailey", "15"],
    ["Colin Thomas", "15"],
    ["Ben Isselstein", "25"],
    ["Baitinger Martin", "36"],
    ["Adrian Garcia", "42"],
    ["Alberto Guglielmi", "42"],
    ["Carl Tremblay", "42"],
    ["Aorta", "42"],
    ["Andreas Gemperle", "42"],
    ["Angel Burns", "42"],
    ["Arno Bosma", "42"],
    ["Christoph Martin Schmid", "42"],
    ["Craig Cameron Olsen", "42"],
    ["Craig Easton", "42"]
]

Fiddle

share|improve this answer

try something like this:

   $(array).sort(function(first, second){
       if(first[0] < second[0]){return -1;}
       else if(first[0] > second[0]){return 1;}
       else if(first[0] === second[0]){
          if(parseInt(first[1]) < parseInt(second[1])){return -1;}
          else return 1;
       }
    });

DEMO

if you vant to sort by number first, use this:

   $(array).sort(function(first, second){
       if(parseInt(first[1]) < parseInt(second[1])){return -1;}
       else if(parseInt(first[1]) > parseInt(second[1])){return 1;}
       else if(parseInt(first[1]) === parseInt(second[1])){
          if(first[0] < second[0]){return -1;}
          else return 1;
       }
    });
share|improve this answer
    
Because this is only sorting it by name (first column). –  putvande Aug 13 at 10:00
    
are you sure. look at 3 else if –  Aleksei Bulgak Aug 13 at 10:01
    
@putvande, firstly it sort using name and if only if name is the same it sort by value –  Aleksei Bulgak Aug 13 at 10:02
    
Did you try it yourself? The name will never be the same so you will never get in that last else if statement. –  putvande Aug 13 at 10:02
    
@putvande, yes. look this –  Aleksei Bulgak Aug 13 at 10:04

I sorted by number

DEMO

 var arr=[
    ["Adrian Garcia", 42],
    ["Alberto Guglielmi", 42],
    ["Alice Fung Yee Ng", 15],
    ["Christopher Wilson",4],
    ["Claudio Meneghetti", 13],
    ["Bahadir Tanriover", 15],
    ["Baitinger Martin", 36],
    ["Bill Cash", 15],
    ["Brian Kuhlmann", 15],
    ["Caesar Lima", 15],
    ["Carl Tremblay", 42],
    ["Aorta", 42],
    ["Charles Harris", 15],
    ["Chet Morrison", 15],
    ["Chico Audi",11],
    ["Andreas Gemperle", 42],
    ["Angel Burns", 42],
    ["Arno Bosma", 42],
    ["Chris Bailey", 15],
    ["Chris Close", 1],
    ["Christoph Martin Schmid", 42],
    ["Ben Isselstein", 25],
    ["Colin Thomas", 15],
    ["Craig Cameron Olsen", 42],
    ["Craig Easton", 42]
]

for (var i = 0; i < arr.length; i++) {// No of times loop executed for given array

    for (var j = 0; j < (arr.length - i) - 1; j++) {   //  it wil save the one value each iteration  

        if (arr[j][1] > arr[j + 1][1]) {
            var temp = arr[j + 1];
            arr[j + 1] = arr[j];
            arr[j] = temp;
        }


    }

}
alert(arr);
share|improve this answer
    
That is half of the answer. –  putvande Aug 13 at 9:57
    
you sort by string not a number –  Grundy Aug 13 at 9:58
    
@Grundy did you debug that code.i am using number not string –  Bala Aug 13 at 10:01
    
i mean that you have as result 1, 11,13 ... instead of 1,4... –  Grundy Aug 13 at 10:02
    
@Grundy ya ,there is one mistake numbers also string changed to number ["Christopher Wilson",4], –  Bala Aug 13 at 10:10

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.