Skip to content
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

Add a sort_posts function #2608

Merged
merged 9 commits into from Dec 28, 2016
Merged

Add a sort_posts function #2608

merged 9 commits into from Dec 28, 2016

Conversation

Kwpolska
Copy link
Member

@Kwpolska Kwpolska commented Dec 27, 2016

This is #2602 — it adds a sort_posts function (available as Jinja filter in global context), which allows general-purpose timeline sorting. Example usage:

sort_posts(timeline, 'title', 'date')
sort_posts(timeline, 'author', reverse=True)

cc @liffiton — does this match your needs?

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Dec 27, 2016

From sort_posts(timeline, 'title', 'date') I'd assume that the posts are sorted by title, and if they have the same title, then they are sorted by date. But the code actually sorts them the other way around: first by date, and only for posts on the same date (i.e. with identical timestamp, as date means timestamp) by title.

@Kwpolska
Copy link
Member Author

@Kwpolska Kwpolska commented Dec 27, 2016

That’s a sane assumption to make, I added a little reversed() call to account for this.

@felixfontein
Copy link
Contributor

@felixfontein felixfontein commented Dec 27, 2016

How about being able to sort some keys reversed, and some not, by prepending reverse_ to the key name? Standard use case is sorting by date and title: you might want to sort by date reversed to get the newest posts first, but by title alphabetically.

@liffiton
Copy link

@liffiton liffiton commented Dec 27, 2016

Looks like a good solution to me. It would certainly handle what I'd like to do (and more).

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
@Kwpolska Kwpolska requested a review from felixfontein Dec 28, 2016
@Kwpolska Kwpolska added this to the v7.8.2 milestone Dec 28, 2016
@Kwpolska
Copy link
Member Author

@Kwpolska Kwpolska commented Dec 28, 2016

@felixfontein done — prepend with - to get a key reversed (same syntax as used in Django). Review welcome.

# An attribute (or method) of the Post object
a = getattr(posts[0], key)
if callable(a):
keyfunc = operator.methodcaller(key)
Copy link
Contributor

@felixfontein felixfontein Dec 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this actually work when key[0] == '-'?

Copy link
Member Author

@Kwpolska Kwpolska Dec 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. Fixed.

Signed-off-by: Chris Warrick <kwpolska@gmail.com>
Copy link
Contributor

@felixfontein felixfontein left a comment

LGTM

@Kwpolska Kwpolska merged commit f592935 into master Dec 28, 2016
4 of 5 checks passed
@Kwpolska Kwpolska deleted the sort_posts branch Dec 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants