2

I have a sort function like this:

function aasort ($array, $key, $order) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    if($order == "asc")
        asort($sorter);
    else
        arsort($sorter);
    foreach ($sorter as $ii => $va){
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

To sort an array depending on 1 of the key values.

and the Array:

$mArray[]   = array(
    'id'  => $v->id,
    'count' => $v->count
);
aasort($mArray, 'count', 'desc');

And the data I get is:

"mArray":
    {
        "1":{"id":"80","count":"2"},
        "0":{"id":"77","count":"1"}
    }

Which I see in the developer tools, but when I copy it in some json editor and check, the problem is they come in the order of index

{
    "0":{"id":"77","count":"1"},
    "1":{"id":"80","count":"2"}
}

.done(function(d){
    for(var x in d.mArray){
      d.mArray[x];
    }
})

Here it takes in the index order which is again 0, 1, ... so the list comes as unsorted.

Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92
Archie.bpgc
  • 23,812
  • 38
  • 150
  • 226
  • 2
    In javascript, object properties order is undetermined. It is unreliable. See [this](http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value). Numeric ones ordered ascending as default. – BlitZ Aug 06 '13 at 09:26
  • 1
    If you want to maintain the sort order then you must encode your data as a JS array. Here `mArray` is an object, which as @CORRUPT says has indeterminate property order. Basically the fix could be as simple as changing `$ret[$ii]=$array[$ii];` to `$ret[]=$array[$ii];`, which would make `json_encode` give you an array later on. – Jon Aug 06 '13 at 09:27
  • 1
    Also `for(var x in d.mArray) d.mArray[x]; }` in @Jon's case would be as `for(var i = 0; i < d.mArray.length; i++){ d.mArray[i] }` – BlitZ Aug 06 '13 at 09:31
  • 1
    Side-note: `reset($array);` could and should be left out: the `foreach` construct resets the internal pointer to the beginning of the array anyway: _"When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop"_ [from the docs](http://php.net/manual/en/control-structures.foreach.php) – Elias Van Ootegem Aug 06 '13 at 09:36
  • @Archie.bpgc: Show us *all* of the relevant code. – Jon Aug 06 '13 at 09:49

1 Answers1

1

Use the key order with caution: when transferring JSON, Chrome will re-sort your indices, whereas Firefox will keep them intact. You can't be sure if the order is kept or not. The only way to solve this is to use

$array = array_values($ret)

or in your code change the last loop to

foreach ($sorter as $ii => $va)
    $ret[]=$array[$ii];

after your sort to create a clean ascending order. If you need the old index you have to store it within your data for each node.

Beryllium
  • 12,808
  • 10
  • 56
  • 86
bitluni
  • 219
  • 1
  • 7