MCPcopy Create free account
hub / github.com/adobe/react-spectrum / sortItemsForDisplay

Function sortItemsForDisplay

packages/dev/s2-docs/src/searchUtils.tsx:699–731  ·  view source on GitHub ↗
(
  items: T[],
  searchValue: string
)

Source from the content-addressed store, hash-verified

697}
698
699export function sortItemsForDisplay<T extends {name: string; href: string; date?: string}>(
700 items: T[],
701 searchValue: string
702): T[] {
703 if (searchValue.trim().length === 0) {
704 return [...items].sort((a, b) => {
705 const aIsIntro = a.href.endsWith('/');
706 const bIsIntro = b.href.endsWith('/');
707
708 // Date sorting for Blog/Releases
709 if (a.date && b.date) {
710 const aDate = new Date(a.date);
711 const bDate = new Date(b.date);
712 return bDate.getTime() - aDate.getTime();
713 } else if (a.date && !b.date) {
714 return 1;
715 } else if (!a.date && b.date) {
716 return -1;
717 }
718
719 // Introduction first
720 if (aIsIntro && !bIsIntro) {
721 return -1;
722 }
723 if (!aIsIntro && bIsIntro) {
724 return 1;
725 }
726 // Sort alphabetically by name
727 return a.name.localeCompare(b.name);
728 });
729 }
730 return items;
731}
732
733export function createSearchOptions<
734 T extends {name: string; tags: string[]; date?: string; section?: string}

Callers 1

getItemsForSectionFunction · 0.85

Calls 1

sortMethod · 0.65

Tested by

no test coverage detected