1
[Object { curr="SEK", antal=7}, Object { curr="JPY", antal=1}, Object { curr="DKK", antal=1}]

I could create a new array and sort it. But I want to sort the array of objects by the antal property. How to do that in pure javascript.

No jquery solutions wanted!

marko
  • 10,684
  • 17
  • 71
  • 92
  • 2
    Which do you want: a sorted array, or the object with the largest value for the "antal" property? The question title says one thing but the text of the question says the other. – Pointy Nov 14 '11 at 15:05
  • possible duplicate of [How to sort an array of objects?](http://stackoverflow.com/questions/979256/how-to-sort-an-array-of-objects) – epascarello Nov 14 '11 at 15:08
  • The `sort` function takes an optional function to define the sort order; you can implement whatever you want. If you just want the largest value, iterate, compare, and save the object with the largest `antal` value. – Dave Newton Nov 14 '11 at 15:08
  • Does not matter. If the largest value is sorted first so I'll grab that. – marko Nov 14 '11 at 15:09
  • Why getting away from jQuery ? :) – Riz Nov 14 '11 at 15:09
  • arr.sort(function(o) { if(o.antal > lblb) {.. } }); Something like this??? ? – marko Nov 14 '11 at 15:10

4 Answers4

3

You can use a custom function to do the sort.

For example:

myArray.sort(function(a, b){
  return a.antal - b.antal;
})

For more information see the tutorial Sorting an array of objects.

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
0
function largestAntal(arr) {
  var rv = null, max = null;
  for (var i = 0; i < arr.length; ++i)
    if (max === null || arr[i].antal > max) {
      max = arr[i].antal;
      rv = arr[i];
    }
  }
  return rv;
}

That'll be faster than sorting if all you want is the object with the biggest "antal" value.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

You can use Array.sort:

var a = [{ curr: "SEK", antal: 7}, { curr: "JPY", antal: 1},  { curr: "DKK", antal: 1}];

a.sort(function(left, right) { return left.antal < right.antal ? 1 : 0; }) 

this will give you:

[Object { curr="SEK", antal=7}, Object { curr="JPY", antal=1}, Object { curr="DKK", antal=1}]

if you want it the other way around, just play with the comparison inside the sort(..) function.

sled
  • 14,525
  • 3
  • 42
  • 70
0

reduce will return a single value with one pass through the array.

var a= [{ curr:"XEK", antal:2 },{ curr:"SEK", antal:7 },{ curr:"JPY", antal:1 },{ curr:"DKK", antal:1 }];

var min= a.reduce(function(a, b){
    return a.antal> b.antal? a:b;
});

*alert('curr='+min.curr+', antal='+min.antal); returned value>> curr= SEK, antal= 7 *

for older browsers you can shim reduce-

if(![].reduce){
    Array.prototype.reduce= function(fun, temp, scope){
        var T= this, i= 0, len= T.length, temp;
        if(typeof fun=== 'function'){
            if(temp== undefined) temp= T[i++];
            while(i < len){
                if(i in T) temp= fun.call(scope, temp, T[i], i, T);
                i++;
            }
        }
        return temp;
    }
}
kennebec
  • 102,654
  • 32
  • 106
  • 127