How to sort a multidimensional array in JavaScript

arrays, javascript, jquery, json

Solution

A bit of correction, it's not a multidimensional array, it's an array of objects, you most likely mixed up the naming from `PHP`, for your question, use the optional function argument of sort to define your own sorting order.

data.sort(function(a, b) {
   return a.percentage - b.percentage;
})

// sorted data, no need to do data = data.sort(...);

http://jsfiddle.net/cEfRd/

Problem

I generally post the code I have so far, but I have nothing on this one... :( How would I go about re-ordering the following array so that I can base it on the value in the 'percentage' key, either low-to-high or high-to-low? ``` var data = [{ "id": "q1", "question": "blah blah blah", "percentage": "32" }, { "id": "q2", "question": "blah blah blah", "percentage": "23" }, { "id": "q3", "question": "blah blah blah", "percentage": "11" }, { "id": "q4", "question": "blah blah blah", "percentage": "3" }, { "id": "q5", "question": "blah blah blah", "percentage": "6" }] ```

Original source