2

I want to sort an array in reverse order of values. I used arsort php function but the result is not good for me.

Example:

I want to sort next array:

myArray = array("d" => 1, "f" => 2, "b" => 3, "c" => 4,  "e" => 2); 

after using arsort php function the result is:

myArray =array ( "c" => 4, "b" => 3, "e" => 2, "f" => 2, "d" => 1 );

which it is not good because the arsort function doesn't keep initial order of elements in array.

I want the result like:

myArray =array ( "c" => 4, "b" => 3, "f" => 2, "e" => 2, "d" => 1 );

f before e order, like from original array. keys with same value to not reverse. Thanks.

Harmen
  • 22,092
  • 4
  • 54
  • 76
Iulian Boiculese
  • 393
  • 1
  • 6
  • 16
  • Quite unusual task, I cannot imagine sensible reason of doing that. However, you can always use usort() to implement a sorting algorithm of any sort – Your Common Sense Dec 04 '10 at 12:55
  • @Sharpnel. The main problem with usort is that you lose the keys in the final product. You will have to use uasort to keep the keys while defining you own sort function. Check the official documentation – denica Dec 04 '10 at 13:20
  • @Col. You again? :) I would like to see a solution written by you using `uasort`. Please enlighten us. – Alin Purcaru Dec 07 '10 at 21:13

6 Answers6

4

Edit: This isn't doable with a built in function and you need to implement your own sorting solution. You can follow this question I opened to understand more.


It is a coincidence that these solutions work:

$myArray = array("d" => 1, "f" => 2, "b" => 3, "c" => 4,  "e" => 2);
uasort($myArray, function($a, $b){
    if($a == $b)
        return 1;
    else
        return $b - $a;
});
print_r($myArray);

or

$myArray = array("d" => 1, "f" => 2, "b" => 3, "c" => 4,  "e" => 2);
uasort($myArray, function($a, $b){
    return $a <= $b;
});
print_r($myArray);
Community
  • 1
  • 1
Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
3

This worked for me.

Since the asort and arsort functions seem to sort rows with equal values by descending key value, an arsort will "switch" a lower key value with a higher if the values are equal.

My solution was to sort low to high (asort), which still does the unwanted row flipping, and then reverse the array which moves the rows with identical values back into their correct respective positions.

orig: Array ( [0] => 1 [1] => 0.5 [2] => 2 [3] => 3 [4] => 0.5 )

sorted: Array ( [4] => 0.5 [1] => 0.5 [0] => 1 [2] => 2 [3] => 3 ) - notice the key values were flipped for the identical valued rows

reversed: Array ( [3] => 3 [2] => 2 [0] => 1 [1] => 0.5 [4] => 0.5 ) - now you have reverse sorted the array and kept key integrity for rows with equal values.

$array=array(1,.5,2,3,.5);
asort($array);
$array=array_reverse($array,TRUE);
0

Please try this

arsort($array, krsort($array));
Pradeep Singh
  • 3,582
  • 3
  • 29
  • 42
  • The second argument to `arsort()` must be a binary flag, not a boolean (regardless of type conversion). And what's the point of doing `krsort()` at all? – BoltClock Dec 04 '10 at 12:58
  • This will definitely *not* do what the OP wants; this will sort the keys first, and the OP wants to avoid sorting keys where the values are equal. – El Yobo Dec 04 '10 at 12:58
0

I think that your best bet will be to use uasort php function. The advantage of using uasort in your case it will be that you have the change to define your own sort algorithm and not be tie to php asort comparison method.

You must define a callback function to evaluate if the first value is less than, greater than or equal to the second value.

You must check the php official online documentation to understand what this function does but I think quite easy to grasp so I will leave my own solution to your problem.

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}

With this callback function you do a reverse sort while keeping the original order of the keys when they have the same value.

Best, Demian

denica
  • 221
  • 1
  • 3
  • 12
0

An easy, anywhere applicable way is:

<?php $a = array_flip($a); ksort($a); $a = array_flip($a); ?>
  1. Array_flip flips each key <==> value.
  2. Sort the array by the "now-keys";
  3. Array_flip again to get the former constellation and a by-value sorted array.

-- P.s.: -- Why being so complicated? @others.

19h
  • 819
  • 9
  • 20
0

It seems that no response is correct for the next array:

$myArray = array( 
    "d" => 1, "Mircea Anca" => 2, 
    "b" => 3, "Iuliana Moise" => 2, 
    "c" => 4, "Florina Popescu" => 2 )
miku
  • 181,842
  • 47
  • 306
  • 310
Iulian Boiculese
  • 393
  • 1
  • 6
  • 16