Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting: assume standard sort is stable, try out the new toSorted method #52213

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions packages/block-editor/src/utils/sorting.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
* Recursive stable sorting comparator function.
*
* @param {string|Function} field Field to sort by.
* @param {Array} items Items to sort.
* @param {string} order Order, 'asc' or 'desc'.
* @return {Function} Comparison function to be used in a `.sort()`.
*/
const comparator = ( field, items, order ) => {
const comparator = ( field, order ) => {
return ( a, b ) => {
let cmpA, cmpB;

Expand All @@ -18,23 +17,14 @@ const comparator = ( field, items, order ) => {
cmpB = b[ field ];
}

let result = 0;
if ( cmpA > cmpB ) {
return order === 'asc' ? 1 : -1;
result = 1;
} else if ( cmpB > cmpA ) {
return order === 'asc' ? -1 : 1;
result = -1;
}

const orderA = items.findIndex( ( item ) => item === a );
const orderB = items.findIndex( ( item ) => item === b );

// Stable sort: maintaining original array order
if ( orderA > orderB ) {
return 1;
} else if ( orderB > orderA ) {
return -1;
}

return 0;
return order === 'asc' ? result : -result;
};
};

Expand All @@ -50,5 +40,5 @@ const comparator = ( field, items, order ) => {
* @return {Array} Sorted items.
*/
export function orderBy( items, field, order = 'asc' ) {
return items.concat().sort( comparator( field, items, order ) );
return items.toSorted( comparator( field, order ) );
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function mapMenuItemsToBlocks( menuItems, level = 0 ) {
let mapping = {};

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

Expand Down
25 changes: 6 additions & 19 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { v4 as uuid } from 'uuid';
/**
* WordPress dependencies
*/
import { createHooks, applyFilters } from '@wordpress/hooks';
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
Expand Down Expand Up @@ -347,24 +347,11 @@ export function getPossibleBlockTransformations( blocks ) {
* @return {?Object} Highest-priority transform candidate.
*/
export function findTransform( transforms, predicate ) {
// The hooks library already has built-in mechanisms for managing priority
// queue, so leverage via locally-defined instance.
const hooks = createHooks();

for ( let i = 0; i < transforms.length; i++ ) {
const candidate = transforms[ i ];
if ( predicate( candidate ) ) {
hooks.addFilter(
'transform',
'transform/' + i.toString(),
( result ) => ( result ? result : candidate ),
candidate.priority
);
}
}

// Filter name is arbitrarily chosen but consistent with above aggregation.
return hooks.applyFilters( 'transform', null );
const priority = ( t ) => t.priority ?? 10;
const candidates = transforms
.filter( ( t ) => predicate( t ) )
.toSorted( ( t1, t2 ) => priority( t1 ) - priority( t2 ) );
return candidates.length > 0 ? candidates[ 0 ] : null;
}

/**
Expand Down
Loading