The majority of posters is incapable of scrolling all the way down to select 6.x-1.19 when they see 6.x-1.9 near the top (see the attached screenshot).

This problem is caused by using the wrong sorting order (1.1 < 1.19 < 1.9) and it also applies to sorting the issues list by the Version column.

Could version_compare() provide the right sorting order (1.1 < 1.9 < 1.19)? Or does it cause other inconsistencies?

Comments

dww’s picture

Title: Use php's version_compare() to sort the content of the Version drop-down box » Stop using project_release_get_releases() to sort the content of the Version drop-down box

Sadly, version_compare() won't help us. However, there's a lot of logic and functionality in Project* for properly sorting complex version strings. Sadly, that logic isn't yet used consistently (it's all been a work-in-progress). See the big @TODO comment above project_release_get_releases() in project_release.module.

What we really need is to remove project_release_get_releases() entirely, and probably further refactor project_release_query_releases_by_branch() into something like project_release_query_releases() that lets you either filter or sort by branch. You still have to define the project id. But, if you leave the $api_tid or $major as NULL, it adds the corresponding DB column to the ORDER BY, not the WHERE (or something like that -- not sure exactly what the most intuitive API would be).

salvis’s picture

StatusFileSize
new945 bytes

The one problem I've seen with version_compare() is that it doesn't sort the 'x' as we need it, but this can be fixed. I've tried the following, taking the release strings from Devel and adding some more:

$releases = array('7.x-1.x-dev', '7.x-1.0-rc2', '7.x-1.0-rc11', '7.x-1.0-rc1', '7.x-1.0-beta1', '7.x-1.0-alpha1', '6.x-1.x-dev', '6.x-1.9', '6.x-1.8', '6.x-1.7', '6.x-1.6', '6.x-1.5', '6.x-1.4', '6.x-1.3', '6.x-1.2', '6.x-1.19', '6.x-1.18', '6.x-1.17', '6.x-1.16', '6.x-1.15', '6.x-1.14', '6.x-1.13', '6.x-1.12', '6.x-1.11', '6.x-1.10', '6.x-1.1', '6.x-1.0', '6.x-0.2', '5.x-1.x-dev', '5.x-1.2', '5.x-1.1', '5.x-1.0', '5.x-0.4', '5.x-0.3', '5.x-0.2', '5.x-0.1', '4.7.x-1.x-dev', '4.6.x-1.x-dev', '4.4.x-1.x-dev', '4.2.x-1.x-dev');

function version_compare_function($a, $b) {
  $a = str_replace('.x-', '.9999-', $a);
  $b = str_replace('.x-', '.9999-', $b);
  if ($a == $b) {
    return 0;
  }
  return version_compare($a, $b, '>') ? -1 : 1;
}

dpm($releases);
uasort($releases, 'version_compare_function');
dpm($releases);

This seems to work perfectly.

I don't have any project data to test it, but the attached patch should put it into practice.

wojtha’s picture

There is same problem in translation interface in localize.drupal.org (l10n_server module). See #790722: Version order in translation form is confusing.

@Salvis Nice, this sorting function seems promising! I used similar approach, but with my own version parser. Usage of version_compare is (at least) undoubtly faster.

sun’s picture

Title: Stop using project_release_get_releases() to sort the content of the Version drop-down box » Sort version lists with version_compare() or natsort()
Priority: Minor » Normal
Status: Active » Needs work

Confirmed, quite annoying.

+++ project_release.module	10 Apr 2010 19:25:13 -0000
@@ -717,9 +717,24 @@
+  if ($a == $b) {
+    return 0;
+  }

We can test $a == $b prior to string replacements already.

Aside from that, this patch makes sense and is badly needed. Not sure whether it fixes all locations where versions are listed though.

--
That said, doesn't natsort() cut it already...?!

# php -r "$a = array('6.x-1.9', '6.x-1.x-dev', '6.x-1.11'); natsort($a); var_dump($a);"

array(3) {
  [0]=>
  string(7) "6.x-1.9"
  [2]=>
  string(8) "6.x-1.11"
  [1]=>
  string(11) "6.x-1.x-dev"
}
salvis’s picture

Status: Needs work » Needs review
StatusFileSize
new589 bytes

@sun: You're right, natsort() does it (almost). array_reverse() does the rest. Much better, of course, thanks!

I have no idea whether there are any other locations that need fixing elsewhere, but I'd be happy to fix this one and see what happens.

As I already mentioned in #2, I can't actually test this because I don't have any project data/installation.

dww’s picture

As I already mentioned in #2, I can't actually test this because I don't have any project data/installation.

http://drupal.org/project/drupalorg_testing

;)

salvis’s picture

Nice, but kind of overkill, given the complexity of the patch. I'm sure someone can test this with less effort...

:)

dww’s picture

@salvis: There's no giant team of "someone" who works in the Project* issue queues testing things. There are 2 maintainers, 1 amazing contributor (right now) and an very tiny group of other people who help out at various times. We're all extremely busy. The whole point of this distro is to make it really trivial to have a useful test site so you don't have an excuse not to test your work. ;) You download a single tarball, unpack it in a webroot, visit install.php and you're done. I hope that's not too much to ask...

salvis’s picture

Title: Sort version lists with version_compare() or natsort() » Sort version lists with version_compare()
StatusFileSize
new942 bytes

Ok, I tested this, and the result is that natsort() doesn't work, because it discards the keys. We need a sort function that preserves the keys, because they are the project version IDs.

So here's the patch from #2 with sun's suggested tweak.

This sorts the drop-down list correctly and keeps it working.

sun’s picture

Does this also affect the version select list on issue comment forms, as visible on this very page?

salvis’s picture

Yes, that's the one I'm targetting. Are there others? Where?

salvis’s picture

@dww: Ok, so in #8 you nudged me into spending another hour on this, with the result that the patch in #9 is pretty much the same as the one in #2 was.

I realize that my time is not as valuable as yours, but it would still be nice to see that I haven't completely wasted it...

salvis’s picture

So, can someone review and RTBC this, please?

tr’s picture

Subscribing. I'll try to test the patch in the next few days.

wojtha’s picture

Salvis's proposed algorithm was commited to the Localize server project so this code has being tested at localize.drupal.org for year now... #790722-6: Version order in translation form is confusing

quicksketch’s picture

Status: Needs review » Reviewed & tested by the community

Patch still applies and works flawlessly. Tested on a local environment with a plethora of version numbers, different API, major, minor, patch, and extra data; everything sorts perfectly. This would be a huge boost to accurate bug reports on drupal.org. I get at *least* 10 reports a week that are improperly filed in the queue because the latest version number is muddled somewhere in the middle of the list. With Drupal core having minor releases exceeding 25, I imagine misfiling is common in the core queue as well.

sun’s picture

Status: Reviewed & tested by the community » Needs review

re: #9 @salvis: natsort() discards keys? That's odd, because the var_dump() in #4 exposes that the array key associations are retained.

> php -r "$a = array('a' => '6.x-1.9', 'z' => '6.x-1.x-dev', 'e' => '6.x-1.11'); natsort($a); var_dump($a);"
array(3) {
  ["a"]=>
  string(7) "6.x-1.9"
  ["e"]=>
  string(8) "6.x-1.11"
  ["z"]=>
  string(11) "6.x-1.x-dev"
}

> php -r "$a = array(619 => '6.x-1.9', 61 => '6.x-1.x-dev', 6111 => '6.x-1.11'); natsort($a); var_dump($a);"
array(3) {
  [619]=>
  string(7) "6.x-1.9"
  [6111]=>
  string(8) "6.x-1.11"
  [61]=>
  string(11) "6.x-1.x-dev"
}
salvis’s picture

You are right, sun. I don't remember what I tested, it's been a while...

Now, natsort() sorts in ascending order — how do we reverse the array without losing the keys?

sun’s picture

array_reverse($versions, TRUE);

@see http://php.net/manual/en/function.array-reverse.php

salvis’s picture

Project: Project issue tracking » Project
StatusFileSize
new488 bytes

Thanks, sun.

This seems to be part of the Project project now.

sun’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!

dww’s picture

Version: 6.x-1.x-dev » 7.x-2.x-dev
Status: Reviewed & tested by the community » Fixed
Issue tags: +needs drupal.org deployment, +project, +drupal.org D7

Sorry for the delay here folks. ;) Life's been crazy. Anyway, we had a similar (only worse) bug in the D7 port, so I just resurrected this patch. Sadly, natsort() is confused by -beta1 and friends. So, we really did want #9, not #20. Committed and pushed to 6.x-1.x and 7.x-2.x branches. Already deployed on the D7 staging site: http://git7site.devdrupal.org/project/issues/views. Tagged it for live d.o deployment, that'll happen soonish.

Thanks!
-Derek

salvis’s picture

Ah, life is great! :-)

Can't wait to see it...

senpai’s picture

The next weekly drupal.org push-to-prd will happen this coming Thursday Oct 25th at 1:00pm PDT. If all is well, it could go out then. See the drupal.org calendar at https://drupal.org/website-schedule

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

drumm’s picture

Issue tags: -needs drupal.org deployment

Deployed.