Req #13825 [Opn->Fbk]: Change sort functions to return sorted array instead of modifying existing

From: Date: Fri, 20 Jan 2017 17:18:38 +0000
Subject: Req #13825 [Opn->Fbk]: Change sort functions to return sorted array instead of modifying existing
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206774@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=13825&edit=1 ID: 13825 Updated by: heiglandreas@php.net Reported by: lee at mediawaveonline dot com Summary: Change sort functions to return sorted array instead of modifying existing -Status: Open +Status: Feedback Type: Feature/Change Request Package: Arrays related Operating System: * PHP Version: * Block user comment: N Private report: N New Comment: Is this still relevant? Previous Comments: ------------------------------------------------------------------------ [2007-03-22 07:26:13] jo at durchholz dot org I agree that there should be functions that return a sorted array instead of sorting in-place. Given the way that PHP handles variables, this should be no more than moderately inefficient. I also find the multitude of sort functions confusing. I always have to look them up. I'd suggest a sort function like this: sorted ($array, $flags, $compare_fn = NULL, $compare_flags = 0) where $flags may be combined from one of each of the following lines (first flag is the default): SORT_REGULAR/SORT_NUMERIC/SORT_STRING These flags define how the values to be sorted (whether it's keys or values) are to be transformed before comparing. SORT_NUMERIC converts the values to be sorted to numbers. SORT_STRING converts them to strings. SORT_VALUES/SORT_KEYS/SORT_VALUES_KEYS/SORT_KEYS_VALUES SORT_KEYS has the effect of ksort. SORT_VALUES_KEYS sorts by values, resorting to keys if values are equal. SORT_KEYS_VALUES sorts by key, then value. This can make sense with SORT_NUMERIC or SORT_STRING if two keys get mapped to the same value. SORT_REINDEX/SORT_KEEP_KEYS Whether to reindex the result array numerically or keep the key-value associations. (Numeric keys probably need to be reindexed always, but I don't know all ramifications here.) SORT_KEEP_KEYS gives the effect of asort. SORT_REGULAR/SORT_REVERSE SORT_REVERSE has the effect of rsort. $compare_fn ($a, $b, $flags) will get the values to be compared in $a and $b, and sorted()s $compare_flags in $flags. For SORT_VALUES, $a and $b will be values from $array. For SORT_KEYS, $a and $b will be keys from $array. For SORT_VALUES_KEYS, $a and $b will be arrays ($value, $key) from a $value=>$key pair in $array. For SORT_KEYS_VALUES, $a and $b will be arrays ($key, $value) from a $value=>$key pair in $array. For $compare_flags, the only flag that could be predefined is SORT_LOCALE. If no $compare_fn is given, the equivalent of this PHP code is executed: <?php if (is_array ($a)) { $result = _compare ($a [0], $b [0]); if ($result == 0) { return _compare ($a [1], $b [1]); } else { return $result; } } else { return _compare ($a, $b); } ?> where _compare is defined to return -1, 0, or +1 depending on how its two parameters compare. Hope this is useful :-) ------------------------------------------------------------------------ [2001-10-25 11:38:43] lee at mediawaveonline dot com $test[0] = 0; $test[1] = 1; $test[2] = 2; asort($test); I hate this, a function should never modify a variable, it should return the new modified variable. they syntax should be $test = asort($test); there are good reasons or this. I hate needing a variable sorted for one command and having to make a temp var just todo that. $tmp = $test; asort($tmp); foreach( $tmp as $pos => $val ) where it could be. foreach( asort($test) as $pos => $val ) saves time and ledgibility. I understand its not easy to change the syntax now. if you wanted to make some work or yourself you could make this a php.ini option. if you want something simpler on your end, just make a new set of functions, thats what Ive done. function my_asort($array) { asort($array); return $array); } even if you dont want to ever include this into base php, at least make asort return the newly modified variable. $tmp = $test; foreach( asort($tmp) as $pos => $val ) Chris Lee lee@mediawaveonline.com ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=13825&edit=1

« previous php.bugs (#206774) next »