开发者

I want to sort an array in reverse order of values in php

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" =>开发者_如何学JAVA 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.


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);


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);


Please try this

arsort($array, krsort($array));


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


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.


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 )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜