Updated: Comment #N

Problem/Motivation

Avoid multiple one off implementations for a common case of sorting by weight and title.

Proposed resolution

Add a SortArray::sortByWeightAndTitleKey method.

Remaining tasks

User interface changes

None

API changes

None, addition of helper method.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

damiankloip’s picture

FileSize
3.25 KB

Here is a start.

damiankloip’s picture

One potential issue: Alot of sort implementations are action on class properties, not arrays. So to make the maximum reuse out of this, maybe we need methods that can work on object properties too?

dawehner’s picture

Awesome test coverage. I wonder whether we can use it somewhere already in the patch.

damiankloip’s picture

FileSize
4.71 KB
2.69 KB

What about something like this?

The last submitted patch, 4: 2151223-8.patch, failed testing.

tim.plunkett’s picture

4: 2151223-8.patch queued for re-testing.

pkunwar’s picture

2151223-8.patch seems to be good.

pkunwar’s picture

Status: Needs review » Reviewed & tested by the community
xjm’s picture

4: 2151223-8.patch queued for re-testing.

catch’s picture

Title: Add a method to SortArray to sort by weight and title » Change notice: Add a method to SortArray to sort by weight and title
Status: Reviewed & tested by the community » Active

Committed/pushed to 8.x, thanks!

Worth adding a change notice for the addition.

tim.plunkett’s picture

Priority: Normal » Critical
Issue tags: +Missing change record, +beta blocker
+++ b/core/lib/Drupal/Component/Utility/SortArray.php
@@ -91,6 +91,34 @@ public static function sortByTitleProperty($a, $b) {
+  public static function sortByWeightAndTitleKey($a, $b, $weight_key = 'weight', $title_key = 'title') {
...
+      return static::sortByKeyString($a, $b, $title_key);

+++ b/core/lib/Drupal/Core/Language/Language.php
@@ -205,14 +205,7 @@ public function extend($obj) {
-        return strnatcasecmp($a->name, $b->name);

Why did we switch from an object with a name property to an array with a title key?

tim.plunkett’s picture

Status: Active » Needs review
FileSize
4.74 KB

Out of curiosity, testing a revert to see what happens.

xjm’s picture

Title: Change notice: Add a method to SortArray to sort by weight and title » Add a method to SortArray to sort by weight and title
Priority: Critical » Normal
Issue tags: -Missing change record, -beta blocker +Needs change record, +Needs tests

Let's wait on the change record until we fix this. :) We can also just create a new draft here and publish it when we commit the fixed patch.

YesCT’s picture

Status: Needs review » Needs work

I think head is silently failing because of missing tests? So this is needs work to get a patch with tests.

damiankloip’s picture

Yeah, seems like we might as well revert this? Not sure why I thought we should add this. It's not that useful really, if you have different keys you need to use.

penyaskito’s picture

I looked at this and I don't know what is the next step.

ArraySort::sortByWeightAndTitleKey it is useful for avoiding duplication, just maybe rename it to sortByWeightAndLabel key and use label by default (I thought there was an issue for that, but cannot find it right now).

In #2239399: Languages should be sorted by label instead of title we are using an anonymous function in sort() for calling this with additional params, so we could use similar patterns in other places.

#11: I don't know, but it allows to reorder arrays and objects in the same way, and reuse sortByKeyInt et al. We could check if this function is needed for arrays anymore and remove the array casting, which probably is a bad thing in terms of performance.

So, as far as I got, @ŧodos:

1. Write a change notice for commit at #10.
2. Check #11, in a follow-up.
3. Rename sortByWeightAndTitleKey() to sortByWeightAndLabelKey(), in a follow-up (which maybe already exists).

Am I right? Still confused.

tim.plunkett’s picture

Title: Add a method to SortArray to sort by weight and title » Add a method to SortArray to sort by weight and title
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs change record, -Needs tests

Other than language, which is broken, there are no other usages of this in core.
I don't think sortByWeightAndTitleKey is actually that reusable.

Even if we had another use case for it, what if it wanted to sort first by Title and *then* by weight?

I think Language::sort should just use SortArray::sortByKeyInt and SortArray::sortByKeyString in the anonymous function.

Reverting this would let us use the test coverage from the other issue, and I think that's enough.

sun’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs change record, +Needs tests

Why did we switch from an object with a name property to an array with a title key?

Because the class is named SortArray...?

YesCT’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs change record, -Needs tests

I agree with reverting this.

sun’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
895 bytes

@tim.plunkett kindly explained the bug/regression to me in IRC.

However, I don't think that really requires a full revert of this patch. The method is useful, but admittedly, it isn't obvious how you can leverage it. The trick is to wrap it into a mini closure, so you can pass custom arguments:

    uasort($languages, function ($a, $b) {
      return SortArray::sortByWeightAndTitleKey($a, $b, 'weight', 'name');
    });

I'm very confident that I'm actively using this method in another issue/patch/branch, but I wasn't able to find it.

YesCT’s picture

tim.plunkett’s picture

Title: Add a method to SortArray to sort by weight and title » Change notice: Add a method to SortArray to sort by weight and title
Priority: Normal » Critical
Status: Needs review » Active
Issue tags: +Needs change record, +Missing change record, +beta blocker

Okay, so this is just back to needing a change notice, and we can follow-up in the language issue.

YesCT’s picture

ok.

YesCT’s picture

Issue summary: View changes

clarified in issue summary remaining tasks.

The last submitted patch, 12: sort-2151223-12.patch, failed testing.

Status: Active » Needs work

The last submitted patch, 20: drupal8.language-sort.20.patch, failed testing.

xjm’s picture

Status: Needs work » Active
catch’s picture

Issue tags: +Novice

Tagging novice for the change record.

catch’s picture

Title: Change notice: Add a method to SortArray to sort by weight and title » Add a method to SortArray to sort by weight and title
Priority: Critical » Normal
Status: Active » Fixed
Issue tags: -Needs change record, -Missing change record, -beta blocker

This is a (very) minor API addition, not an API change at all, so I don't really think it's worth adding a change notice for.

Status: Fixed » Closed (fixed)

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

YesCT’s picture