1

I use javascript dictionary for a big list , and complete it like under code

var child = { id:1, values:30 }; var array = [] ; array.push(child); 

I want sorting this array by value field my problem is that I can't find a javascript method for it I can write it manually, but it is my last hope

4
  • 3
    Try array.sort(function(a,b){return a.values-b.values}) Commented Feb 3, 2014 at 8:20
  • 1
    possible duplicate of Iterate over a Javascript associative array in sorted order Commented Feb 3, 2014 at 8:22
  • Please google your problem before asking about it on SO. Sorting dicts has been done numerous times before, and there is plenty of info about it available. Commented Feb 3, 2014 at 8:23
  • i google it and i dont find any answer for sorting list of dictionary by a dictionary keys Commented Feb 3, 2014 at 8:54

1 Answer 1

2

You'll need a custom callback (aka comparerfunction) for the sorting. Something along the lines of 1:

array.sort( function (a,b) { return +a.value - +b.value; } ); 

1 I'm assuming value should be numeric and to be sure the sorting will be numeric the value property is explicitly converted to a number using +

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

1 Comment

Those + aren't necessary because String - String is String - +String, which then means String - Number and a String subtracting a number will cast the String to a Number.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.