# How to sort an array of objects?

**URL:** <https://forum.kirupa.com/t/how-to-sort-an-array-of-objects/285980>\
**Category:** Uncategorized\
**Created:** [April 8, 2009, 1:23pm UTC](https://forum.kirupa.com/t/how-to-sort-an-array-of-objects/285980 "2009-04-08T13:23:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![betaman37](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@betaman37](https://forum.kirupa.com/u/betaman37)\
**Post date:** [April 8, 2009, 1:23pm UTC](https://forum.kirupa.com/t/how-to-sort-an-array-of-objects/285980/1 "2009-04-08T13:23:58Z")

</div>

using this code…

```auto

var carsArray:Array = new Array();
carsArray.push({car:"mazda", year:1982, options:{price:15000, rating:"C"}});
carsArray.push({car:"ford", year:2001, options:{price:25000, rating:"A"}});
carsArray.push({car:"volkswagen", year:2001, options:{price:13000, rating:"A"}});
carsArray.push({car:"volkswagen", year:1997, options:{price:14000, rating:"F"}});
carsArray.push({car:"volkswagen", year:1997, options:{price:15000, rating:"A"}});
carsArray.push({car:"volkswagen", year:1997, options:{price:18000, rating:"B"}});

carsArray.sortOn(["car", "year", "rating"]);
for (var car in carsArray) {
    for (var prop in carsArray[car]) {
        trace(" " + prop + ": " + carsArray[car][prop]);
        for (var option in carsArray[car][prop]) {
            trace(" " + option + ": " + carsArray[car][prop][option]);
        }
    }
    trace("---");
}

```

which traces…

```auto

  options: [object Object]
    price: 25000
    rating: A
  car: ford
  year: 2001
---
  options: [object Object]
    price: 15000
    rating: C
  car: mazda
  year: 1982
---
[COLOR=Red] [COLOR=DarkRed]options: [object Object]
    price: 14000
    rating: F
  car: volkswagen
  year: 1997
---[/COLOR][/COLOR][COLOR=DarkRed]
[/COLOR] [COLOR=DarkRed] options: [object Object]
    price: 15000
    rating: A
  car: volkswagen
  year: 1997
---
[/COLOR] [COLOR=DarkRed] options: [object Object]
    price: 18000
    rating: B
  car: volkswagen
  year: 1997
---[/COLOR]
  options: [object Object]
    price: 13000
    rating: A
  car: volkswagen
  year: 2001
---

```

how would i get this output, so that the sort is by **car, year, then by rating**?

The 1997 volkswagens should start from A to F, like this…

```auto

  options: [object Object]
    price: 25000
    rating: A
  car: ford
  year: 2001
---
  options: [object Object]
    price: 15000
    rating: C
  car: mazda
  year: 1982
---
 [COLOR=DarkRed] options: [object Object]
    price: 15000
    rating: A
  car: volkswagen
  year: 1997
---
  options: [object Object]
    price: 18000
    rating: B
  car: volkswagen
  year: 1997
***---
***[/COLOR] [COLOR=DarkRed] options: [object Object]
    price: 14000
    rating: F
  car: volkswagen
  year: 1997
---[/COLOR]
  options: [object Object]
    price: 13000
    rating: A
  car: volkswagen
  year: 2001
---

```

i’ve tried all different ways, but the rating syntax never takes effect.  
thanks!
