0

Below, I have an array that looks like JSON data. I get this result by querying something and pushing it to var test = [];. The way I append the strings and numbers into my test array is by running test.push({item_name: name, number_of_items: number});. Just to be clear, it is not JSON:

[{"item_name":"Peri Peri Chicken","number_of_items":6},{"item_name":"Deep fried beef","number_of_items":22},{"item_name":"Roast chicken","number_of_items":7},{"item_name":"Chow Mein","number_of_items":3},{"item_name":"Sweet and sour noodles","number_of_items":5},{"item_name":"Steak","number_of_items":32}]

Now, I want to sort the array so that it shows the array from the largest number_of_items to the smallest number_of_items.

I have tried to sort it like you would do with a JSON array, but it doesn't work as I have realised that I cannot access the elements in that array by using test.number_of_items. I tried THIS method to sort the array, but it doesn't work for me as my data is not in JSON.

It would also really helpful if someone can tell me if there's a better way to append multiple data into the same index of an array, and also to be able to access the elements easier in the future? so for example, on index 1 I would have something like (elem1: string1, elem2: number1) and then index 2 (elem1: string2, elem2: number2) and ...

1
  • It is JSON (in your example, JSON is just a notation). Your array elements are objects. Your objects can be addressed by array index, your values with those object elements can be addressed by key. Really basic JSON and Javascript syntax stuff so just practice. Commented Mar 29, 2017 at 23:06

2 Answers 2

2

Try this :

  test = [{"item_name":"Peri Peri Chicken","number_of_items":6},   {"item_name":"Deep fried beef","number_of_items":22},{"item_name":"Roast chicken","number_of_items":7},{"item_name":"Chow Mein","number_of_items":3},    {"item_name":"Sweet and sour noodles","number_of_items":5},{"item_name":"Steak","number_of_items":32}];

  test.sort(function(a, b) {
    return a.number_of_items - b.number_of_items;
  });

execution image Good luck

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

3 Comments

number_of_items is already a number, no need to parseInt()
@n00dl3, my bad
Thanks, this code worked! I've been struggling with this for a long time.
0

You can parse it as JSON, order it, and turn it back in to a string, like this:

var array = [
    { "item_name": "Peri Peri Chicken", "number_of_items": 6 },
    { "item_name": "Deep fried beef", "number_of_items": 22 },
    { "item_name": "Roast chicken", "number_of_items": 7 },
    { "item_name": "Chow Mein", "number_of_items": 3 },
    { "item_name": "Sweet and sour noodles", "number_of_items": 5 },
    { "item_name": "Steak", "number_of_items": 32 }
];

var compare = (a,b) => {
  if (a.number_of_items < b.number_of_items)
    return 1;
  if (a.number_of_items > b.number_of_items)
    return -1;
  return 0;
};

var test = JSON.parse(array);
test.sort(compare);
array = JSON.stringify(test);

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.