13

I have a Multidimensional Array which has 3 columns (by using javascript)

[0] Number of vote
[1] Name of candidate
[2] Candidate Number

My array contents are:

1 | Peter | 3
1 | Mary  | 2
0 | David | 5
0 | John  | 4
0 | Billy | 1

How can I sort the array by [0] Number of vote and then [2] candidate number?

The result should be:

1 | Mary  | 2
1 | Peter | 3
0 | Billy | 1
0 | John  | 4
0 | David | 5
user2314737
  • 27,088
  • 20
  • 102
  • 114
Joe Yan
  • 2,025
  • 8
  • 43
  • 62
  • Possible duplicate of : http://stackoverflow.com/questions/2784230/javascript-how-do-you-sort-an-array-on-multiple-columns – DhruvPathak Aug 09 '11 at 08:29
  • or http://stackoverflow.com/questions/6101475/how-does-one-sort-a-multi-dimensional-array-by-multiple-columns-in-javascript – DhruvPathak Aug 09 '11 at 08:30

6 Answers6

20

As previously said, you should use a custom sort function. Here's one that would do exactly what you want.

var arr = [];
arr[0] = [1, 'Peter', 3];
arr[1] = [1, 'Mary', 2];
arr[2] = [0, 'David', 5];
arr[3] = [0, 'John', 4];
arr[4] = [0, 'Billy', 1];

arr.sort(function (a,b) {
    if (a[0] < b[0]) return  1;
    if (a[0] > b[0]) return -1;
    if (a[2] > b[2]) return  1;
    if (a[2] < b[2]) return -1;
    return 0;
});
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
Bojan Bjelic
  • 3,522
  • 1
  • 17
  • 18
6
array.sort( function (a,b) {
    if (a[0] > b[0]) return  1;
    if (a[0] < b[0]) return -1;
    if (a[2] > b[2]) return  1;
    if (a[2] < b[2]) return -1;
    return 0;
});
Shock
  • 501
  • 2
  • 9
1

Here is a generic function

function arraySort(pArray)
{
pArray.sort(
  function(a,b)
  {
    var len=a.length;
    for (var i=0;i<len;i++)
    {
      if (a[i]>b[i]) return 1;
      else if (a[i]<b[i]) return -1;
    }
    return 0;
  }
);
}
Nilesh
  • 2,015
  • 3
  • 19
  • 21
1

To sort a multi-dimensional array comparing by coordinate 0 first and then by component 2 you can combine two compare functions with ||:

compare 0-coordinates || compare 2-coordinates

For numeric values simply use a difference as a compare function, as x - y is:

  • 0 if x == y
  • >0 if x > y
  • <0 if x < y

Adjust the order of the elements in the differences to take care of ascending/descending order.

var myArray = [
  [1, 'Peter', 3],
  [1, 'Mary', 2],
  [0, 'David', 5],
  [0, 'John', 4],
  [0, 'Billy', 1]
];

myArray.sort(function(a, b) {
  return b[0] - a[0] || a[2] - b[2];
});

console.log(JSON.stringify(myArray));
user2314737
  • 27,088
  • 20
  • 102
  • 114
0

Another method is to create a value for each array entry, e.g. 1000*number of votes + candidate number, so that this value is unambiguous and unique, e.g. we get 1003, 1002, 5, 4, 1. Add a key refering back to the original array and sort.

So we would sort [[1003,0],[1002,1],[5,2],[4,3],[1,4]] by the first element of each subarray.

There is a discrepancy in your sorting system, you use high->low for votes and low-high for candidate number.

JMP
  • 4,417
  • 17
  • 30
  • 41
0

As far as I know you will have to make your own sorting function.
If you can store it in a object than defining a function like in the previous answer will do the job.
Refer Sort array of objects

Shadow
  • 6,161
  • 3
  • 20
  • 14