1

I need to sort the values miles and price based on perMile.

objs[i] = [
    {
        miles : array[i].value1
    },  
    {
        price: array[i].value2
    },
    {
        perMile: array[i].value3
    }
];

I am new to JavaScript

msturdy
  • 10,479
  • 11
  • 41
  • 52
user1561245
  • 5
  • 1
  • 5
  • 2
    Your data structure doesn't make sense. Why do you have an array of objects where each object has a different property? Why not just have one object? – Felix Kling Aug 05 '13 at 08:02

2 Answers2

0

I would suggest

objs[i] = {
    miles : array[i].value1,
    price: array[i].value2,
    perMile: array[i].value3
};

Then you can sort the objs array by the perMile properties of the items like this (assuming they're numbers):

objs.sort(function(a, b) {
    return a.perMile - b.perMile;
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
-1

If you can use a library in this instance, you should try Jlinq - http://hugoware.net/Projects/jlinq - It's a library that will allow you to query a Javascript object (associative array) using SQL-esque methods (SORT, SELECT, FROM, etc). The examples on the site will help you to get started. It's a great library, well doucmented, and very lightweight.

Charlie
  • 4,197
  • 5
  • 42
  • 59