Skip to content

Commit 7760bb7

Browse files
jsnajdrtyxlaaduthmanzoorwanijkgeriux
authored
Sorting: assume standard sort is stable, try out the new toSorted method (#52213)
* Use the newly-standard .toSorted method * Use toSorted in findTransform * Update changelogs Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org> Co-authored-by: aduth <aduth@git.wordpress.org> Co-authored-by: manzoorwanijk <manzoorwanijk@git.wordpress.org> Co-authored-by: geriux <geriux@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
1 parent e831782 commit 7760bb7

15 files changed

Lines changed: 28 additions & 36 deletions

File tree

‎packages/babel-plugin-makepot/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Raise the minimum required Node.js version to `>=22.13.0`, for stable `require(esm)` support ([#84313](https://github.com/WordPress/gutenberg/pull/84313)).
88

9+
### Internal
10+
11+
- Use the standard `Array.prototype.toSorted()` method instead of copying an array before sorting ([#52213](https://github.com/WordPress/gutenberg/pull/52213)).
12+
913
## 6.57.0 (2026-10-07)
1014

1115
## 6.56.0 (2026-09-23)

‎packages/babel-plugin-makepot/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ function isSameTranslation( a, b ) {
190190
* @return {Array} Sorted translations.
191191
*/
192192
function sortByReference( translations = [] ) {
193-
return [ ...translations ].sort( ( a, b ) =>
193+
return translations.toSorted( ( a, b ) =>
194194
a.comments.reference.localeCompare( b.comments.reference )
195195
);
196196
}

‎packages/block-editor/src/utils/sorting.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const comparator = < T extends SortItem >(
4545
return order === 'asc' ? -1 : 1;
4646
}
4747

48-
// `Array.prototype.sort` is stable, so ties keep their original order.
48+
// `Array.prototype.toSorted` is stable, so ties keep their original order.
4949
return 0;
5050
};
5151
};
@@ -66,5 +66,5 @@ export function orderBy< T extends SortItem >(
6666
field: SortField< T >,
6767
order = 'asc'
6868
) {
69-
return items.concat().sort( comparator( field, order ) );
69+
return items.toSorted( comparator( field, order ) );
7070
}

‎packages/block-library/src/gallery/order-images.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export function sortImageBlocks( blocks, media, { orderby, order } ) {
9797
] )
9898
);
9999

100-
return [ ...blocks ].sort( ( a, b ) => {
100+
return blocks.toSorted( ( a, b ) => {
101101
const keyA = keys.get( a );
102102
const keyB = keys.get( b );
103103
if ( keyA === undefined || keyB === undefined ) {

‎packages/block-library/src/navigation/menu-items-to-blocks.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function mapMenuItemsToBlocks( menuItems, level = 0 ) {
3434
let mapping = {};
3535

3636
// The menuItem should be in menu_order sort order.
37-
const sortedItems = [ ...menuItems ].sort(
37+
const sortedItems = menuItems.toSorted(
3838
( a, b ) => a.menu_order - b.menu_order
3939
);
4040

‎packages/block-library/src/playlist/edit.jsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ const PlaylistEdit = ( {
323323

324324
const onChangeOrder = useCallback(
325325
( trackOrder ) => {
326-
const sortedBlocks = [ ...innerBlockTracks ].sort( ( a, b ) => {
326+
const sortedBlocks = innerBlockTracks.toSorted( ( a, b ) => {
327327
const titleA = a.attributes.title || '';
328328
const titleB = b.attributes.title || '';
329329

‎packages/blocks/src/api/factory.ts‎

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { v4 as uuid } from 'uuid';
2-
import { createHooks, applyFilters } from '@wordpress/hooks';
2+
import { applyFilters } from '@wordpress/hooks';
33
import deprecated from '@wordpress/deprecated';
44
import warning from '@wordpress/warning';
55
import {
@@ -460,24 +460,12 @@ export function findTransform< T extends BlockTransform >(
460460
transforms: T[],
461461
predicate: ( transform: T ) => boolean
462462
): T | null {
463-
// The hooks library already has built-in mechanisms for managing priority
464-
// queue, so leverage via locally-defined instance.
465-
const hooks = createHooks();
466-
467-
for ( let i = 0; i < transforms.length; i++ ) {
468-
const candidate = transforms[ i ];
469-
if ( predicate( candidate ) ) {
470-
hooks.addFilter(
471-
'transform',
472-
'transform/' + i.toString(),
473-
( result ) => ( result ? result : candidate ),
474-
candidate.priority
475-
);
476-
}
477-
}
478-
479-
// Filter name is arbitrarily chosen but consistent with above aggregation.
480-
return hooks.applyFilters( 'transform', null ) as T | null;
463+
// `priority` has the same meaning as in WP hooks: lower runs earlier, default is 10.
464+
const priority = ( t: T ) => t.priority ?? 10;
465+
const candidates = transforms
466+
.filter( ( t ) => predicate( t ) )
467+
.toSorted( ( t1, t2 ) => priority( t1 ) - priority( t2 ) );
468+
return candidates.length > 0 ? candidates[ 0 ] : null;
481469
}
482470

483471
/**

‎packages/editor/src/components/collab-sidebar/utils.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ export function calculateNotePositions( {
722722
// sweep's assumption holds and cards never displace past their markers.
723723
// Threads without a rect keep their relative order; they are skipped
724724
// below and never receive a position.
725-
const orderedThreads = [ ...threads ].sort(
725+
const orderedThreads = threads.toSorted(
726726
( a, b ) =>
727727
( blockRects[ a.id ]?.top ?? Number.MAX_VALUE ) -
728728
( blockRects[ b.id ]?.top ?? Number.MAX_VALUE )

‎packages/editor/src/components/collaborators-presence/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function CollaboratorsPresence( {
4545

4646
// Always include self in the list sorted first.
4747
const collaboratorsForList = useMemo( () => {
48-
return [ ...activeCollaborators ].sort( ( a, b ) => {
48+
return activeCollaborators.toSorted( ( a, b ) => {
4949
if ( a.isMe && ! b.isMe ) {
5050
return -1;
5151
}

‎packages/editor/src/components/post-taxonomies/flat-term-selector.jsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function FlatTermSelector( { slug } ) {
121121
...DEFAULT_QUERY,
122122
// Sort ids so reordering alone doesn't produce a new query key and re-fetch.
123123
include: _termIds?.length
124-
? [ ..._termIds ].sort( ( a, b ) => a - b ).join( ',' )
124+
? _termIds.toSorted( ( a, b ) => a - b ).join( ',' )
125125
: undefined,
126126
per_page: -1,
127127
};

0 commit comments

Comments
 (0)