-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add sort_numeric filter #1028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add sort_numeric filter #1028
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -193,6 +193,23 @@ def where(input, property, target_value = nil) | |
| end | ||
| end | ||
|
|
||
| # Sort elements of an array in numeric order | ||
| # provide optional property with which to sort an array of hashes or drops | ||
| def sort_numeric(input, property = nil) | ||
| ary = InputIterator.new(input) | ||
| if property.nil? | ||
| ary.sort do |a, b| | ||
| Utils.to_number(a) <=> Utils.to_number(b) | ||
| end | ||
| elsif ary.empty? # The next two cases assume a non-empty array. | ||
| [] | ||
| elsif ary.first.respond_to?(:[]) && !ary.first[property].nil? | ||
| ary.sort do |a, b| | ||
| Utils.to_number(a[property]) <=> Utils.to_number(b[property]) | ||
| end | ||
| end | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If none of these
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could result in broken liquid being more obviously broken (i.e. showing |
||
| end | ||
|
|
||
| # Remove duplicate elements from an array | ||
| # provide optional property with which to determine uniqueness | ||
| def uniq(input, property = nil) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this unnecessary (because
ary.first.respond_to?(:[])will befalseifaryis emptyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but wouldn't the
elsebranch returnnilinstead of[]as this branch does?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep I'm an idiot