-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"]
]
RajivRisi
  • 398
  • 1
  • 12

3 Answers3

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;
       }
    });
Aliaksei Bulhak
  • 6,078
  • 8
  • 45
  • 75
  • Because this is only sorting it by name (first column). – putvande Aug 13 '14 at 10:00
  • are you sure. look at 3 else if – Aliaksei Bulhak Aug 13 '14 at 10:01
  • @putvande, firstly it sort using name and if only if name is the same it sort by value – Aliaksei Bulhak Aug 13 '14 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 '14 at 10:02
  • Names are in alphabetic order, numbers are not in order: http://jsfiddle.net/xugnadz8/1/ (same code but different way of login so you can actually see what the array looks like). – putvande Aug 13 '14 at 10:07
  • @putvande, you kidding me. I write that if values(strings) not equal only then we check numbers – Aliaksei Bulhak Aug 13 '14 at 10:09
  • Again, `else if(first[0] === second[0]){` will never happen because there are no duplicates of names in there. Put an `alert` or a `console.log` in that if statement and see for yourself. – putvande Aug 13 '14 at 10:09
  • @putvande, yes. this is normal situation. you use second argument for sorting when first not unique. If you create list with duplicates of names and different numbers my algo sort them correctly. so I write correct algo and you downvote me. You can't sort by name and number at the same time – Aliaksei Bulhak Aug 13 '14 at 10:11
  • @putvande, so what. do you agree with my answer? – Aliaksei Bulhak Aug 13 '14 at 10:24
  • Yes your update orders them first by number and then by name. So I have changed my down in an upvote. – putvande Aug 13 '14 at 10:29
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

putvande
  • 15,068
  • 3
  • 34
  • 50
-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);
Balachandran
  • 9,567
  • 1
  • 16
  • 26