5

I have this array in jquery:

var myArray = new Array();
myArray[1] = new Array();
myArray[2] = new Array();
myArray[3] = new Array();
myArray[4] = new Array();
myArray[5] = new Array();

The element myArray[x][1] (x can be 1 to 5) contains a numeric value (a price) and the element myArray[x][2] another numeric value (an identifier). I need to order the array by the value of myArray[x][1] (the price) without separating every price from his identifier (his [x][2]).

How do i?

Arul Dinesh
  • 550
  • 4
  • 15
Sasha Grievus
  • 2,566
  • 5
  • 31
  • 58
  • http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects?rq=1 – Alex Sep 24 '13 at 09:47
  • @Alex, That isn't the same thing – musefan Sep 24 '13 at 09:48
  • @musefan: It's *very nearly* the same thing, as arrays are objects. But probably not an *exact* duplicate. – T.J. Crowder Sep 24 '13 at 09:51
  • @T.J.Crowder: I wouldn't think it wise to try and access object properties by using an index, but I suppose the answers are relevant (assuming the OP can see the similarities between the different code examples) – musefan Sep 24 '13 at 09:54
  • @musefan: It **is** slightly advanced, no question. Note that with standard JavaScript arrays, "indexes" are actually property names, because standard JavaScript arrays [aren't really arrays at all](http://blog.niftysnippets.org/2011/01/myth-of-arrays.html). – T.J. Crowder Sep 24 '13 at 09:56
  • @T.J.Crowder: Yes, of course. But if we call everything what it really is in javascript then we don't have many words to describe anything. – musefan Sep 24 '13 at 10:00

1 Answers1

7

You can use Array#sort for that:

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

...assuming that the [1] entries in the sub-arrays are in fact numbers.

See live example below.


Side note #1: Note that array indexes start at 0, so your array as quoted has undefined (rather than a sub-array) in the first position. You'll want to fix that before doing the above, possibly by doing the below.


Side note #2: In JavaScript, there is almost never any reason to write new Array(). Instead, just use an array literal: []. Your quoted code, for instance, could be:

var myArray = [
    [],
    [],
    [],
    [],
    []
];

...assuming you fix the error identified above.


Live Example | Live Source:

// [0] is the id, [1] is the price
var myArray = [
    [1, 14.95],
    [2, 7.50],
    [3, 8.99],
    [4, 12.25],
    [5, 13.72]
];

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

var index, entry;
for (index = 0; index < myArray.length; ++index) {
    entry = myArray[index];
    console.log(index + ": " + entry[0] + " - " + entry[1]);
}

Output:

0: 2 - 7.5
1: 3 - 8.99
2: 4 - 12.25
3: 5 - 13.72
4: 1 - 14.95
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • work perfectly, you saved the day! ^^ I admit i've not understood completely what's going on, but i will take my time to do it. Thank you a lot. – Sasha Grievus Sep 24 '13 at 10:31
  • 1
    @Elisabetta: The `Array#sort` function accepts a function that it will call repeatedly to compare entries from the array while sorting it. It passes the function two args (the entries to compare). The function should return a negative value if the first should "before" the second, `0` if the two entries are equivalent, or a positive number if the second should be "before" the first. So if you're comparing by price, and if price is the second entry (`[1]`) in each sub-array, then `a[1] - b[1]` is negative if `a[1]` is `<` `b[1]`, `0` if they're the same, or positive if `b[1]` is `>` `a[1]`. – T.J. Crowder Sep 24 '13 at 11:39
  • cool, i got the point! Thank you very much. I was able to get all to work perfectly. ^^ – Sasha Grievus Sep 24 '13 at 12:32
  • 1
    I think StackOverflow should implement "Thank you so much" button for an answer like this :). – Zalom Jun 20 '18 at 16:00
  • @Zlatko - I think that's the Upvote button. :-) (But you need more rep first.) – T.J. Crowder Jun 20 '18 at 16:14
  • @Zlatko - LOL, my bad, I thought you needed more rep. But [the site tells me otherwise](https://stackoverflow.com/help/privileges/vote-up). – T.J. Crowder Jun 21 '18 at 11:58
  • @T.J.Crowder All I needed is an idea. More tools in my toolbelt. Thank you for that. I am not chasing rep although I am a long time member. Nice to hear from you btw. It is good that you are so active. – Zalom Jun 21 '18 at 12:04