0

Ok I got an array of objects in an array. Well they are actually associative arrays. So its actually an array of associative arrays.

console log outputs something like this:

[array object] [object{...},object{...},object{...}, etc...]

Each object has a name, price and color. What I want is to sort them into alphabetical order and ascending or descending in price.

I've got a jQuery code that will put them onto the page, but I need to insert a bit of code that will put them in the order I choose. Any ideas?

3
  • 1
    there's no such thing as "associative array" in javascript: that is an array of objects. You need to write your custom sort function developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… (scroll about half page) Commented Sep 20, 2014 at 16:52
  • There is no such build-in method for array sorting in JS until you write an algorithm by yourself, instead you can use LINQ and do arrayObject.orderBy(x => x), try linqjs.codeplex.com Commented Sep 20, 2014 at 17:06
  • @VishalSachdeva: Of course there is: Array#sort. All you have to provide is the comparator, not the sorting algorithm. Commented Sep 20, 2014 at 17:41

2 Answers 2

3

You can use the javascript array sort method that accepts a compare function as parameter.

var a = [{name: 'a', price: 1, color: 'red'},
{name: 'b', price: 2, color: 'red'},
{name: 'c', price: 3, color: 'blue'}];
// Ascending order
a.sort(function (a, b) {
    return a.price - b.price;
});
// Descending order
a.sort(function (a, b) {
    return b.price - a.price;
});

EDIT: If you need to sort first by name in ascending order and then by price in ascending or descending order then:

// Sort by name in ascending order first and then by price in ascending order
a.sort(function (a, b) {
    return a.name.localeCompare(b.name) || a.price - b.price;
});
// Sort by name in ascending order first and then by price in descending order
a.sort(function (a, b) {
    return a.name.localeCompare(b.name) || b.price - a.price;
});
Sign up to request clarification or add additional context in comments.

2 Comments

@T.J.Crowder Didn't see the name first sorting. Fixed.
I've never seen it done with || like that before, clever! I know the special way JavaScript's || works, I just never thought to apply it to this.
0

You can use Array#sort to sort arrays. It accepts a comparator callback function (a function the sort algorithm can use to compare two entries to see which should come first) and sorts the array in place (e.g., it doesn't make a copy of the array). The contract is: The comparator gets called with two entries, and returns <0 if the first should come before the second, 0 if they're equal, and >0 if the first should come after the second:

yourArray.sort(function(a, b) {
    // return <0 if a is less than b
    //         0 if a is equal to b
    //        >0 if a is greater than b
});

You can use String#localeCompare to compare the names alphabetically; it conveniently returns <0, 0, or >0. If the comparison of the names doesn't return 0, return what it did return; if the comparison of names returns 0, return a.price - b.price.

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.