php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #765 new kusort() function (or similar)
Submitted: 1998-09-21 20:32 UTC Modified: 1998-09-25 12:18 UTC
From: libs at cybercable dot tm dot fr Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 3.0.3 OS: Linux (irrelevant)
Private report: No CVE-ID: None
 [1998-09-21 20:32 UTC] libs at cybercable dot tm dot fr
I'd be delighted if a new function similar to ksort
would appear in PHP3.

1/The requested functionality would be the following one:
1.1/Consider an array:
$toto["001"]="a";
$toto["010"]="c";
$toto["100"]="d";
$toto["002"]="b";
$toto["110"]="e";

1.2/Sorting this array on its key with the requested
function would give:
key : value
001 : a
002 : b
010 : c 
100 : d 
110 : e 

1.3/Due to variable typing, ksort behaves differently
and sorts this way:
key : value
010 : c 
100 : d 
110 : e
001 : a
002 : b

1.4/There is no practical way of getting the requested
functionality with the existing functions.

Prepending zero or appending a dot work, of course,
but it lacks elegance, especially when you have to strip
the zero or the dot afterwards for use in the rest of 
the script.

2/Suggested ways of achieving the requested functionality.

2.1/ kusort() function.

A kusort() function, modeled after ksort like usort is
modeled after sort. This would be the most versatile
solution.

2.2/ kasort() function.

This key sort would assume that we deal with an associative
array and all keys are strings. It would not change the 
types of keys inside the array, just the comparisons in
the sorting algorithm.
It is less versatile but just what I need, so I assume
other people will find it useful too.

2.3/ Add an optionnal argument to ksort() for type of
comparison.

Like so: ksort($array, "integer") or ksort($array, "string")

3/ Conclusion.

Coders are choosers. Anything would make me happy, with 
a preference for kasort()

I failed to be concise, but I hope I managed to be clear.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1998-09-25 12:18 UTC] zeev
kusort() has been implemented, and named uksort().
The other suggestsions are too specific to your problem,
and aren't of much use in the vast majority of cases.
The uksort() solution can be used to implement any key-based
sort order.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 14:01:30 2024 UTC