2

There is strange issue in php asort, arsort.

I am taking example of arsort

Case1

$a = array(
    1 => 2,
    2 => 1,
    3 => 2,
    4 => 1
);
arsort($a);
var_dump($a);

Output:

array(4) {
  [3] =>
  int(2)
  [1] =>
  int(2)
  [4] =>
  int(1)
  [2] =>
  int(1)
}

Here at index (3,1) and (4,2) are sorted in descending order because at index 3 and 1 values are same. Same for index 4 and 2.

Case2

$a = array(
    1 => 2,
    2 => 1,
    3 => 2
);
arsort($a);
var_dump($a);

Output:

array(3) {
  [1] =>
  int(2)
  [3] =>
  int(2)
  [2] =>
  int(1)
}

Here at index (3,1) are sorted in ascending order and still at index 3 and 1 values are same.

Is there any solution for this issue? As I want that ordering should be certain. Like sort in either descending or ascending order if value at some indices are same.

Charles
  • 50,943
  • 13
  • 104
  • 142
Gaurav
  • 28,447
  • 8
  • 50
  • 80
  • @kingkero : Thank you. Probably I got the solution from you link. – Gaurav Jan 02 '14 at 12:19
  • Well, if values compare as equal the order of the keys is undefined by definition, as they're not part of the thing being sorted on. If I understand you correctly, you want to be able to sort on value, and if that compares as equal, you want to sort on the key? Or do you not want to sort on key, but on the original position in the array? (Which could be 2 very different things). – Wrikken Jan 02 '14 at 12:20
  • The strange thing is, that they are **always** different, so there is some kind of way, how the `arsort` sorts them. – Peon Jan 02 '14 at 12:23
  • I want to sort according to value in descending order and order of key should be in same order (either descending or ascending). – Gaurav Jan 02 '14 at 12:24

2 Answers2

5

According to the PHP documentation:

If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable).

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
0

You can't know exactly which behaviour is correct, with testing just with 2 elements. Here's an array with multiple elements (odd and even).

even number:

<?php

$a = array(
    1 => 2,
    2 => 1,
    3 => 2,
    4 => 1,
    5 => 2,
    6 => 1,
    7 => 2,
    8 => 1,
    9 => 2,
    10 => 1,
    11 => 2,
    12 => 1
);
arsort($a);
var_dump($a);

Result:

array (size=12)
  1 => int 2
  7 => int 2
  5 => int 2
  11 => int 2
  9 => int 2
  3 => int 2
  10 => int 1
  12 => int 1
  6 => int 1
  2 => int 1
  4 => int 1
  8 => int 1

odd number

<?php

$a = array(
    1 => 2,
    2 => 1,
    3 => 2,
    4 => 1,
    5 => 2,
    6 => 1,
    7 => 2,
    8 => 1,
    9 => 2,
    10 => 1,
    11 => 2,
    12 => 1,
    13 => 2
);
arsort($a);
var_dump($a);

Result

array (size=13)
  9 => int 2
  11 => int 2
  13 => int 2
  1 => int 2
  7 => int 2
  3 => int 2
  5 => int 2
  12 => int 1
  2 => int 1
  4 => int 1
  8 => int 1
  6 => int 1
  10 => int 1

The question is now, where did it add the 13th element (=2)? it's added just after the 11th element and before the first element...this means that there's no rule here. (At least according to what we see).

We can't say that it follows any rule with testing with just 2 variables like what you did, because you saw (1,3) and you presumed that it's sorted by key. Which is apparently not the case with multiple variables.

SmootQ
  • 2,096
  • 7
  • 33
  • 58