@carbon/react
Version:
React components for the Carbon Design System
31 lines (29 loc) • 1.2 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
//#region src/components/MultiSelect/tools/sorting.ts
const isSelectAllItem = (item) => typeof item === "object" && item !== null && "isSelectAll" in item;
/**
* Use `localeCompare` with the `numeric` option enabled to sort two
* alphanumeric strings.
*/
const defaultCompareItems = (itemA, itemB, { locale }) => itemA.localeCompare(itemB, locale, { numeric: true });
/**
* Default sorting function for options in a selection control.
*/
const defaultSortItems = (items, { selectedItems, itemToString, compareItems, locale }) => {
return items.sort((itemA, itemB) => {
if (isSelectAllItem(itemA) && itemA.isSelectAll) return -1;
if (isSelectAllItem(itemB) && itemB.isSelectAll) return 1;
const hasItemA = selectedItems.includes(itemA);
const hasItemB = selectedItems.includes(itemB);
if (hasItemA && !hasItemB) return -1;
if (hasItemB && !hasItemA) return 1;
return compareItems(itemToString(itemA), itemToString(itemB), { locale });
});
};
//#endregion
export { defaultCompareItems, defaultSortItems };