php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #48305 SORT_VERSION for sort functions
Submitted: 2009-05-16 18:49 UTC Modified: 2014-05-01 06:49 UTC
From: sean at wdsolutions dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: Any OS: Irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-05-16 18:49 UTC] sean at wdsolutions dot com
Description:
------------
It would be awesome to have a SORT_VERSION flag for the sort family of functions, whose behavior would be to sort most common multi-number version strings (with possible support for detecting -cvs, -rc, -svn, and other suffixes).

For example, SORT_NUMERIC and SORT_STRING both fail to properly sort the following version numbers:

1.2.1
1.2.2
1.2.10
1.20.5
1.22.50

They get sorted as:

1.20.5
1.2.1
1.2.10
1.2.2
1.22.50

Code I used to work around it is below:

Reproduce code:
---------------
function vercmp($a, $b) {
  $as = explode('.', $a);
  $bs = explode('.', $b);
  for ($i = 0, $e = max(count($as), count($bs)); $i != $e; ++$i) {
    if ($as[$i] < $bs[$i])
      return -1;
    elseif ($as[$i] > $bs[$i])
      return 1;
  }
  return 0;
}



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-05-18 12:12 UTC] a at b dot c dot de
You could use the existing "version_compare" function instead of writing your own. That would cover the PHP version-numbering convention. Other conventions could be addressed by hand-written functions, but given the existence of those other conventions a single "SORT_VERSION" flag wouldn't be sufficient.
 [2014-04-30 22:53 UTC] levim@php.net
-Status: Open +Status: Feedback -Package: Feature/Change Request +Package: *General Issues -Operating System: Linux +Operating System: Irrelevant -PHP Version: 5.2.9 +PHP Version: Any
 [2014-04-30 22:53 UTC] levim@php.net
Is this something you are still interested in seeing?
 [2014-05-01 06:49 UTC] nikic@php.net
-Status: Feedback +Status: Wont fix
 [2014-05-01 06:49 UTC] nikic@php.net
As pointed out by the first comment, this can already be accomplished by passing version_compare to usort.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 21:01:33 2024 UTC