uasort() does NOT preserve sort order, but core assumes so (Element::children, css sort, ...)

Created on 6 April 2015, over 9 years ago
Updated 1 April 2023, over 1 year ago

Meta split from #2448765: Element::children sort order undefined and slower than it could be - This makes tests fail in PHP7 .

Problem/Motivation

- Core uses uasort in quite a few places, but uasort does not preserve the sort order

Hence while:

$test = [
  'foo:1' => [ '#markup' => 'foo - first' ],
  'bar:2' => [ '#markup' => 'bar - second' ],
];

works

$test = [
  'foo:1' => [ '#markup' => 'foo - first' ],
  'bar:2' => [ '#markup' => 'bar - second' ],
  'first:0' => [ '#markup' => 'little things', '#weight' => -1000 ],
];

does not work when run via Element::children($test, TRUE); (until #2448765: Element::children sort order undefined and slower than it could be - This makes tests fail in PHP7 is committed)

It returns:

Array
(
    [0] => first:0
    [1] => bar:2
    [2] => foo:1
)

which is wrong.

This affects Drupal 7 as well as 8 and is the reason why explicit weights are needed so often in 7 even though it _should_ work without.

Proposed resolution

- Add a generic drupalUASort functionality that does preserve the weights.
- Convert all users of uasort to this helper class to ensure we don't have inconsistency in core with very weird weights bugs later.

Remaining tasks

- Discuss
- Fix generically

User interface changes

API changes

🐛 Bug report
Status

Needs work

Version

10.1

Component
Base 

Last updated about 6 hours ago

Created by

🇩🇪Germany Fabianx

Live updates comments and jobs are added and updated live.
  • DrupalWTF

    Worse Than Failure. Approximates the unpleasant remark made by Drupal developers when they first encounter a particular (mis)feature.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Triaged core major

    There is consensus among core maintainers that this is a major issue. Only core committers should add this tag.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024