M
M
Max Developer2017-01-31 15:16:46
PHP
Max Developer, 2017-01-31 15:16:46

How to process (sort) data received from an external json file?

I need to display in html the already sorted data from the json file
how to display it - there are no problems, but how to sort it depending on different data by key - I can’t find and understand,
advise who knows what

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kovalevm, 2017-01-31
@kovalevm

In the compare function, you specify the conditions for sorting objects. compare should always return 1|0|-1 and nothing else.

function compare(a,b) {
  if (a.OrderID < b.OrderID)
    return -1;
  if (a.OrderID > b.OrderID)
    return 1;
  return 0;
}

Then you pass this function to the sort array method, and the array is sorted.
arr.sort(compare);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question