usort

usort changing Array's order

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-02 06:10:17
问题 I have a usort function with a single line: return 0. I tried to use it on an Array of stdClass objects, and it changes their order, how is that possible? 回答1: The property you assume is called stability: A stable sorting algorithm will not change the order of elements that are equal. php's sorting functions are not stable (because non-stable sorts may be slightly faster). From the documentation of usort: If two members compare as equal, their order in the sorted array is undefined. If you

usort changing Array's order

▼魔方 西西 提交于 2020-01-02 06:10:08
问题 I have a usort function with a single line: return 0. I tried to use it on an Array of stdClass objects, and it changes their order, how is that possible? 回答1: The property you assume is called stability: A stable sorting algorithm will not change the order of elements that are equal. php's sorting functions are not stable (because non-stable sorts may be slightly faster). From the documentation of usort: If two members compare as equal, their order in the sorted array is undefined. If you

Properly sorting multidimensional array using usort and spaceship operator

自古美人都是妖i 提交于 2019-12-31 04:02:29
问题 I need to sort the following array with following logic: If the 'score' is the same, I want to compare using 'time'. The array is as follows: $user_scores = [ 82 => [ 'score' => 1, 'time' => 6.442 ], 34 => [ 'score' => 1, 'time' => 5.646 ], 66 => [ 'score' => 3, 'time' => 1.554 ] ] The 'keys' in the above array are the 'user_ids', which I need to preserve in the sorted array. My best attempt so far is as below - $result = usort( $user_scores, function ( $a, $b ) { if ( $a['score'] == $b[

Access array key using uasort in PHP

▼魔方 西西 提交于 2019-12-30 03:13:48
问题 If have a rather basic uasort function in PHP that looks like this: uasort($arr, function($a, $b) { if ($a > $b) return -1; if ($a < $b) return 1; ... } The array I'm trying to sort looks like the following: {[1642] => 1, [9314] => 4, [1634] => 3 ...} It contains integers that are my main comparison criteria. However, if the integers are equal, then I would like to access their key values, inside the uasort function and do some magic with it to figure out the sorting from there. I have no

Sort associative array according to value without deleting keys [duplicate]

北城余情 提交于 2019-12-24 13:38:31
问题 This question already has answers here : Sort array in PHP by value and maintain index association (2 answers) Closed last year . I have an array which i want to sort on the basis of its value. But when i'm using rsort , it deletes all the keys. I tried to flip array and then used krsort , but then it removes some key/values pairs which have the same key. Array ( [533ae5a78ead0e8e118b4567] => 1 [534d5a4b8ead0e5b73294d72] => 45 [533ee8bc8ead0ec5138b4567] => 32 [535f42748ead0ef72ec72731] => 1

Sort multi-dimensional array with usort

家住魔仙堡 提交于 2019-12-24 09:58:56
问题 The following usort function does not always give the right result since it will only "push" up or down one position relative to the compared item. Thus when performing the sort multiple times the result Yes No Yes No can occur. The function successfully sort field b. How can I solve this? array [0] => array("a"=>"Yes","b"=>"apple"...); [1] => array("a"=>"Yes","b"=>"banana"...); [2] => array("a"=>"No","b"=>"lemon"...); [3] => array("a"=>"No","b"=>"grape"...); ... current function function

How to understand usort logic?

寵の児 提交于 2019-12-24 02:51:11
问题 I'm trying to understand how php function usort works. I have such code: <?php $users[] = array('login' => 'moon', 'name' => 'Chris'); $users[] = array('login' => 'star', 'name' => 'Piter'); $users[] = array('login' => 'mars', 'name' => 'Tim'); $users[] = array('login' => 'earth', 'name' => 'Garry'); function compare($a, $b) { echo $a['login'] . '--' . $b['login'] . '<br />'; echo strcmp($a['login'], $b['login']) . '<br />'; return strcmp($a['login'], $b['login']); } usort($users, "compare");

Why isn't my usort() output sorted?

夙愿已清 提交于 2019-12-24 02:23:35
问题 I'm rewriting an old script that spits out the most popular content using usort. For some reason, the output of my usort isn't actually sorted. I'm using php 5.5 (please disregard the use of the depreciated mysql_ function, that is part of the reason I am rewriting this script). //store data in array $sort_array = array(); while($row = mysql_fetch_assoc($result)) { //calculate age $age = (strtotime("now") - strtotime($row["DATE"]))/86400;//86400 converts seconds to days //calculate "effective

Help me sort this php array using usort()

╄→尐↘猪︶ㄣ 提交于 2019-12-24 00:24:30
问题 I have a data structure that looks like Array ( [0] => Array ( [0] => something [1] => 1296986500 ) [1] => Array ( [0] => something else [1] => 1296600100 ) [2] => Array ( [0] => another thing [1] => 1296831265 ) ) I'm trying to sort the array based off of the integer which is a unix timestamp. The following function looks right to me but is not sorting the way I want. function cmp($a, $b) { if ($a[1] == $b[1]) { return 0; } return ($a[1] < $b[1]) ? -1 : 1; } NOTE when calling this function

PHP usort won't sort class

岁酱吖の 提交于 2019-12-23 06:56:57
问题 This is a sample of the array of elemnts to sort: $items = array 0 => object(stdClass)[8] public 'id' => string '110' (length=3) public 'brand_id' => string '18' (length=2) array 0 => string ' OT-708' (length=7) public 'failed' => null public 'diff' => null 1 => object(stdClass)[9] public 'id' => string '161' (length=3) public 'brand_id' => string '18' (length=2) So, let's say I want to sort by brand_id . This is my usort callback function: function _compare($itemA, $itemB){ if ($itemA->brand