// 1. Decorate: Compute the heavy operation exactly ONCE per item
const decorated = items.map(item => ({
item,
sortKey: heavyParsingOperation(item.dateStr) // e.g., Date.parse()
}));
// 2. Sort: Fast, lightweight numeric comparison
decorated.sort((a, b) => a.sortKey - b.sortKey);
// 3. Undecorate: Extract original items
const sortedItems = decorated.map(entry => entry.item);
Top comments (1)