V
V
vohaha2015-11-01 00:22:08
JavaScript
vohaha, 2015-11-01 00:22:08

How to sort data received by ajax?

I get an array of such objects

{
  "ID": "1",
  "NAME": "\u0422\u043e\u0432\u0430\u04401",
  "PRICE": 2,
  "BRAND": "brand1",
  "DESCRIPTION": "\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435",
  "IMG": "address",
  "IMG2": "",
  "WEIGHT": 1,
  "VOLUME": 1
}

I output using handlebars. What is the best way to implement sorting "by price (lower first)", etc.?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
FMars, 2015-11-01
@FMars

Why is the standard function not suitable?
Array.prototype.sort()

var items = [
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic' },
  { name: 'Zeros', value: 37 }
];
items.sort(function (a, b) {
  if (a.value > b.value) {
    return 1;
  }
  if (a.value < b.value) {
    return -1;
  }
  // a must be equal to b
  return 0;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question