Moodle
  1. Moodle
  2. MDL-22936

Perform natural sort on groups and groupings

    Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.9.9, 2.0
    • Fix Version/s: DEV backlog
    • Component/s: Groups
    • Labels:
      None
    • Affected Branches:
      MOODLE_19_STABLE, MOODLE_20_STABLE
    • Rank:
      6099

      Description

      Groups and groupings are currently sorted using a basic alphanumeric sort (as the sorting is done by the DBMS), which fails to sort names with numbers in them correctly when the number of digits differs. For example, if you use the auto-create groups feature to create 20 groups using the naming scheme "Group #" it'll create "Group 1" through "Group 20" - but they'll appear in lists in the order "Group 1, Group 10, Group 11, ..., Group 19, Group 2, Group 20, Group 3, ..." which isn't ideal.

      I've attached patches against Moodle 1.9.9 and Moodle 2.0 (cvshead from git.moodle.org/moodle.git) which implement natural sorting on groups and groupings. I may have missed any places that they're listed outside the groups/groupings admin area, as I haven't done an exhaustive search of Moodle for lists of groups/groupings).

      Note that you won't be able to get to the Groupings or Overview tabs on 2.0 until MDL-22903 is resolved (unless you manually patch it), and that the list of groups assigned to the selected member in the group assignment list will not appear on 2.0 until MDL-22935 is resolved. These features will all still be sorted correctly by the attached patch.

        Activity

        Hide
        Petr Škoda added a comment -

        hello, this would break really badly in languages that use non-ascii characters because strnatcasecmp() supports one collation

        workaround is simple "Group 01"

        Show
        Petr Škoda added a comment - hello, this would break really badly in languages that use non-ascii characters because strnatcasecmp() supports one collation workaround is simple "Group 01"
        Hide
        Paul Nicholls added a comment -

        Good point, Petr - but making the auto-create groups system add leading zeroes doesn't seem like the "nicest" (from a user's perspective) way to fix it, especially if you have 100+ groups - and it won't affect existing groups.

        Perhaps what we need to do is simply add a new (numeric) column which gets populated automatically by the auto-create groups system, and sort by that first and the group name second? It could be exposed as an advanced option in the group also, so that the sort order of manually-created groups can be tweaked if necessary. The field could be left null (or set to 0, to achieve more consistent results on different DMBSes - I think some may sort nulls before all else and others after all else) for manually-created groups without sort orders specified, and the auto-create system could then grab the maximum sort for groups in the relevant context and start at the next index.

        While this approach wouldn't necessarily result in all groups being sorted (auto-created groups would be in one block after all manual groups with no explicit sort order set), it would resolve the issue for auto-created groups, which are the primary issue. It also would not break any 3rd-party blocks/modules which list groups, since they'd just continue to sort in the existing manner if not updated. Groupings could simply have an advanced setting for sort order - that might be nice to add either way, to give a little more control.

        Alternatively, the auto-create system could be tweaked to use two columns - a (string) "prefix" stored in the existing group name column, and an integer index stored in a separate column for groups auto-created using the "#" placeholder (those using "@" for letters could still be stored as the group name only). Groups could then be sorted by name followed by index. It does, however, mean that the check for group name uniqueness would need to be modified to check the suffix as well as the name, since you'd end up with several groups with the same name but unique indexes. The # denoting the position of the number within the group name could remain in the database, so the number goes into the correct spot.

        This approach does require post-processing of the results in order to replace the placeholders, which may cause some performance degradation, though, and will require changes anywhere that groups are fetched/listed (preferably to use a library function to fetch groups which handles this). Such a change would probably be best done in 2.0 only, since it could easily break existing functionality in 3rd-party blocks/modules (they'd end up showing a bunch of "Group #" items).

        Show
        Paul Nicholls added a comment - Good point, Petr - but making the auto-create groups system add leading zeroes doesn't seem like the "nicest" (from a user's perspective) way to fix it, especially if you have 100+ groups - and it won't affect existing groups. Perhaps what we need to do is simply add a new (numeric) column which gets populated automatically by the auto-create groups system, and sort by that first and the group name second? It could be exposed as an advanced option in the group also, so that the sort order of manually-created groups can be tweaked if necessary. The field could be left null (or set to 0, to achieve more consistent results on different DMBSes - I think some may sort nulls before all else and others after all else) for manually-created groups without sort orders specified, and the auto-create system could then grab the maximum sort for groups in the relevant context and start at the next index. While this approach wouldn't necessarily result in all groups being sorted (auto-created groups would be in one block after all manual groups with no explicit sort order set), it would resolve the issue for auto-created groups, which are the primary issue. It also would not break any 3rd-party blocks/modules which list groups, since they'd just continue to sort in the existing manner if not updated. Groupings could simply have an advanced setting for sort order - that might be nice to add either way, to give a little more control. Alternatively, the auto-create system could be tweaked to use two columns - a (string) "prefix" stored in the existing group name column, and an integer index stored in a separate column for groups auto-created using the "#" placeholder (those using "@" for letters could still be stored as the group name only). Groups could then be sorted by name followed by index. It does, however, mean that the check for group name uniqueness would need to be modified to check the suffix as well as the name, since you'd end up with several groups with the same name but unique indexes. The # denoting the position of the number within the group name could remain in the database, so the number goes into the correct spot. This approach does require post-processing of the results in order to replace the placeholders, which may cause some performance degradation, though, and will require changes anywhere that groups are fetched/listed (preferably to use a library function to fetch groups which handles this). Such a change would probably be best done in 2.0 only, since it could easily break existing functionality in 3rd-party blocks/modules (they'd end up showing a bunch of "Group #" items).
        Hide
        Tobias Marx added a comment -

        I just wanted to add, that the same problem applies to course names.

        Show
        Tobias Marx added a comment - I just wanted to add, that the same problem applies to course names.
        Hide
        Rex Lorenzo added a comment -

        Paul, I know this patch is 2 years old, but if you still want to have it in you can use lib/textlib.php's asort function to naturally sort group/groupings names: http://phpdocs.moodle.org/HEAD/moodlecore/textlib.html#asort

        Seems like a patch we can use at our site, but no one's really complained about it on our end yet though.

        Show
        Rex Lorenzo added a comment - Paul, I know this patch is 2 years old, but if you still want to have it in you can use lib/textlib.php's asort function to naturally sort group/groupings names: http://phpdocs.moodle.org/HEAD/moodlecore/textlib.html#asort Seems like a patch we can use at our site, but no one's really complained about it on our end yet though.
        Hide
        Paul Nicholls added a comment -

        Thanks, Rex - I'll put that on my long list of things to investigate!

        Show
        Paul Nicholls added a comment - Thanks, Rex - I'll put that on my long list of things to investigate!

          People

          • Votes:
            7 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated: