Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By joelpittet on
Change record status:
Draft (View all draft change records)
Project:
Introduced in branch:
8.0.x
Introduced in version:
8.0.3
Issue links:
Description:
Summary
uasort()
does NOT preserve sort order though we presumed it does in many locations in core. For example: Element::children()
We've added a new method to help preserve the sort order.
Considering this render array:
$elements = [
0 => [
'#type' => 'checkbox',
'#title' => 'A',
],
1 => [
'#type' => 'checkbox',
'#title' => 'B',
],
2 => [
'#type' => 'checkbox',
'#title' => 'C',
'#weight' => -1000,
],
];
Sorted by weight is expected to be sorted as keys 2, 0, 1
Before
uasort($elements, ['Drupal\Component\Utility\SortArray', 'sortByWeightProperty']);
print implode(', ', array_keys($elements));
Produces:
2, 1, 0
After
SortArray::stableUasort($elements, ['Drupal\Component\Utility\SortArray', 'sortByWeightProperty']);
var_dump(array_keys($elements));
Produces:
2, 0, 1
Impacts:
Module developers