Skip to content

feat(orderBy, sortBy): improve orderBy, sortBy - #1552

Closed
jidohyun wants to merge 4 commits into
toss:mainfrom
jidohyun:feat/sortby-string-array
Closed

jidohyun wants to merge 4 commits into
toss:mainfrom
jidohyun:feat/sortby-string-array

Conversation

@jidohyun

@jidohyun jidohyun commented Dec 9, 2025 •

Copy link
Copy Markdown

This PR improve the orderBy, sortBy function, as requested in the issue(#869)

Added:

  • sortBy and orderBy string array support: Enhanced both functions to work with primitive arrays (strings, numbers, etc.) by removing T extends object constraint
  • Optional criteria parameters: Made criteria parameter optional with default empty array, enabling direct primitive sorting without explicit functions
  • Optional orders parameter: Added default ascending order to orderBy for simplified usage
  • Comprehensive test cases: Added tests for string arrays, number arrays, empty criteria, and mixed sorting scenarios using consistent ['apple', 'banana', 'cherry'] pattern
  • Enhanced documentation: Updated JSDoc with primitive array examples and usage patterns for both Korean and English docs
  • Type safety improvements: Maintained full type inference while supporting broader input types
  • Backward compatibility: Preserved all existing object sorting functionality without breaking changes

Here is the screenshot of the performance benchmark results:
Screenshot 2025-12-09 at 12 20 33 PM
Screenshot 2025-12-09 at 12 21 03 PM

- Remove T extends object constraint to support all types including primitives
- Add optional criteria parameter with default empty array for primitive sorting
- Add optional orders parameter with default ascending order in orderBy
- Update JSDoc to document primitive array support with examples
- Add comprehensive test cases for string and number arrays
- Support both explicit criteria (functions) and automatic primitive sorting
- Maintain backward compatibility with existing object sorting functionality
@vercel

vercel Bot commented Dec 9, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
es-toolkit Ready Ready Preview Comment Dec 9, 2025 5:16am

@jidohyun jidohyun closed this Dec 9, 2025
@jidohyun jidohyun reopened this Dec 9, 2025
@raon0211

raon0211 commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the PR!

However, for arrays of strings or other primitives, I think using the native sort directly is much more natural and better ergonomics:

arr.sort((x, y) => x.localeCompare(y));

sortBy and orderBy are designed for sorting arrays of objects by their properties, and extending them to primitives adds complexity to the API without a clear benefit over what modern JavaScript already provides. This also aligns with our design principle of not implementing features that are easily replaceable by modern JS.

So I'll close this PR. Thanks again for your contribution and interest in es-toolkit!

This branch was successfully deployed

1 active deployment
Preview — 805da23f Deployed Dec 9, 2025 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants