0

I got an array of objetcs from PHP via json_encode. The problem is that on PHP side items are ordered but when it gets on JS side, items are not in order.

myArray[96] = array ( "product" => "Drone",
"quant" => "23",
"available" => "true");

myArray[43] = array ( "product" => "Aereo",
"quant" => "2",
"available" => "false");

myArray[55] = array ( "product" => "Geos",
"quant" => "45",
"available" => "true");

myArray[13] = array ( "product" => "Barsad",
"quant" => "3",
"available" => "true");

So, how can I order that array by "product" on JavaScript side?

marcospereira
  • 12,045
  • 3
  • 46
  • 52
Leonardo
  • 39
  • 1
  • 7
  • Have you tried to do this using a loop? – Salman Jan 20 '16 at 00:24
  • What do you want to order by? You could write your own bubble sort algorithm and sort your associative array by some key, for example in quantity highest to lowest. There is plenty of documentation online on how to write sorting algorithms, you could write your own simple one but if you are sorting large data sets you may want to look at more complex sorting algorithms to improve efficiency – Halfpint Jan 20 '16 at 00:24
  • 1
    Please show the actual Javascript array that you want to sort, not the PHP. In Javascript the `Array.prototype.sort()` method with a custom sort function passed to it is probably what you want. – jfriend00 Jan 20 '16 at 00:25
  • 2
    It seems you have an associative array in PHP, which maps to an object in JSON (which is unordered). Reindex your array elements before you convert them to JSON and they should be encoded as array, thus preserving the order (e.g. `json_encode(array_values($array)))`). – Felix Kling Jan 20 '16 at 00:25
  • Possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – djechlin Jan 20 '16 at 00:57

1 Answers1

3

The problem is the fact that what PHP calls "array" can be, but can also not be an array in JavaScript. You are getting an object (or associative array, or dictionary, or hashtable):

{"96":{"product":"Drone","quant":"23","available":"true"},
 "43":{"product":"Aereo","quant":"2","available":"false"},
 "55":{"product":"Geos","quant":"45","available":"true"}...}

which is not orderable in JavaScript. You have three options: convert to a true array (if you don't care about keys, or if you can insert the keys into the objects), use an index array (and sort that instead), or (in ES6) use Map and its contract that it shall preserve the order of items in order of insertion (but it only works on new JavaScript environments).

For example, one way would be this (using an index array):

var data = {"96":{"product":"Drone","quant":"23","available":"true"},
     "43":{"product":"Aereo","quant":"2","available":"false"},
     "55":{"product":"Geos","quant":"45","available":"true"}};
var keys = Object.keys(data);
keys.sort(function(a, b) {
  var pa = data[a].product;
  var pb = data[b].product;
  if (pa === pb) return 0;
  return (pa < pb) ? -1 : 1;
});
keys.forEach(function(key) {
  console.log(key, data[key]);
});
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Amadan, first of all, thanks you for helped me. It works with that index array although it is not very elegant and practical. And, indeed, I need the keys for products in order to have direct access to them. Thanks again!!! – Leonardo Jan 20 '16 at 14:51