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!

6
  • 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. Commented Nov 14, 2011 at 15:05
  • possible duplicate of How to sort an array of objects? Commented Nov 14, 2011 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. Commented Nov 14, 2011 at 15:08
  • Does not matter. If the largest value is sorted first so I'll grab that. Commented Nov 14, 2011 at 15:09
  • Why getting away from jQuery ? :) Commented Nov 14, 2011 at 15:09

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

your sort returns the lowest value first
Good point. To sort in descending order, a and b need to be flipped, as per your solution.
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.

Comments

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.

Comments

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

Comments

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.