Creates a comparator function for numerical sorting with multi-value
support.
Generates a comparator that extracts numerical values from objects and
performs mathematical comparison. Supports both single numbers and arrays
of numbers for complex numerical sorting scenarios like sorting by price
then by rating.
When comparing arrays, performs lexicographic numerical ordering: compares
the first numbers, then the second numbers if the first are equal, and so
on. This enables sophisticated sorting like "sort by price ascending, then
by rating descending".
// Multi-field: category, then price products.sort(GaffComparator.numbers(product=> [product.categoryId, product.price]));
// Complex business logic: popularity (sales) then rating products.sort(GaffComparator.numbers(product=> [-product.salesCount, -product.rating])); // Negative values for descending order
// Sort by inventory priority: low stock first, then by sales products.sort(GaffComparator.numbers(product=> [product.stock, -product.salesCount]));
// Test multi-criteria sorting constsortByBusinessValue = GaffComparator.numbers(product=> [ -product.salesCount, // High sales first -product.rating, // High rating first product.price// Low price first (for tie-breaking) ]);
Creates a comparator function for numerical sorting with multi-value support.
Generates a comparator that extracts numerical values from objects and performs mathematical comparison. Supports both single numbers and arrays of numbers for complex numerical sorting scenarios like sorting by price then by rating.
When comparing arrays, performs lexicographic numerical ordering: compares the first numbers, then the second numbers if the first are equal, and so on. This enables sophisticated sorting like "sort by price ascending, then by rating descending".
Example