-2

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"]
]
9
  • 1
    I tried most of the solutions -- Show us what you tried. Commented Aug 13, 2014 at 9:50
  • All that solutions are sort with only single value. Ex: myArray.sort(function(a, b) { return a[1] - b[1]; }); Commented Aug 13, 2014 at 9:53
  • @RajivRisi so you can use not single value in sort function :-) Commented Aug 13, 2014 at 9:53
  • 1
    OP, this has nothing to do with jQuery. Just plain JavaScript. Commented Aug 13, 2014 at 9:57
  • 1
    Can you show what the output should look like? Commented Aug 13, 2014 at 10:09

3 Answers 3

2

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;
       }
    });
Sign up to request clarification or add additional context in comments.

10 Comments

Because this is only sorting it by name (first column).
are you sure. look at 3 else if
@putvande, firstly it sort using name and if only if name is the same it sort by value
Did you try it yourself? The name will never be the same so you will never get in that last else if statement.
Names are in alphabetic order, numbers are not in order: jsfiddle.net/xugnadz8/1 (same code but different way of login so you can actually see what the array looks like).
|
1

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

Comments

-1

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

5 Comments

That is half of the answer.
you sort by string not a number
@Grundy did you debug that code.i am using number not string
i mean that you have as result 1, 11,13 ... instead of 1,4...
@Grundy ya ,there is one mistake numbers also string changed to number ["Christopher Wilson",4],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.