Type-safe comparator functions for Array.sort() operations with advanced field access.

GaffComparator provides a collection of specialized comparator functions designed to work seamlessly with Array.sort() and testing frameworks like TestValidator.sort(). Each comparator supports both single values and arrays of values, enabling complex multi-field sorting scenarios with lexicographic ordering.

Key features:

  • Generic type safety for any object structure
  • Support for single values or arrays of values per field
  • Lexicographic comparison for multi-value scenarios
  • Locale-aware string comparison
  • Automatic type conversion for dates and numbers

The comparators follow the standard JavaScript sort contract:

  • Return < 0 if first element should come before second
  • Return > 0 if first element should come after second
  • Return 0 if elements are equal

Jeongho Nam - https://github.com/samchon

  // Basic usage with single fields
users.sort(GaffComparator.strings(user => user.name));
posts.sort(GaffComparator.dates(post => post.createdAt));
products.sort(GaffComparator.numbers(product => product.price));

// Multi-field sorting with arrays
users.sort(GaffComparator.strings(user => [user.lastName, user.firstName]));
events.sort(GaffComparator.dates(event => [event.startDate, event.endDate]));

// Integration with TestValidator
await TestValidator.sort("user sorting")(
(sortable) => api.getUsers({ sort: sortable })
)("name", "email")(
GaffComparator.strings(user => [user.name, user.email])
)("+");

Functions

dates
numbers
strings