Flask Mailinglist

« back to archive

Jinja2 - Descending order in groupby and dictsort filters

Jinja2 - Descending order in groupby and dictsort filters

From:
Clodoaldo Neto
Date:
2012-03-23 @ 11:40
Is there a way to make either of groupby or dictsort filters to sort in
reverse order?

The last dictsort example in the doc includes the words "sorted normally"
suggesting there would be a "sorted abnormally" option:

{% for item in mydict|dictsort(false, 'value') %}    sort the dict by
key, case insensitive, sorted    normally and ordered by value.


As for groupby there is no mention of sorting order option.

Regards, Clodoaldo

Re: Jinja2 - Descending order in groupby and dictsort filters

From:
Simon Sapin
Date:
2012-03-23 @ 12:04
Le 23/03/2012 12:40, Clodoaldo Neto a écrit :
> Is there a way to make either of groupby or dictsort filters to sort in 
> reverse order? 
> 
> The last dictsort example in the doc includes the words "sorted 
> normally" suggesting there would be a "sorted abnormally" option: 
> 
> {%  for  item  in  mydict|dictsort(false,  'value')  %} 
>      sort the dict by key, case insensitive, sorted 
>      normally and ordered by value. 


Hi,

The current dictsort filter does not do that:

https://github.com/mitsuhiko/jinja2/blob/master/jinja2/filters.py#L187

However you could easily add a "reversed" argument (defaults to False) 
to the filter that would just be passed to sorted()

The best would be to send a pull request, but until the feature is 
merged and released you can override the filter:

app.add_template_filter(reversable_dictsort, name='dictsort')


> As for groupby there is no mention of sorting order option. 

Same story.

Regards,
--  
Simon Sapin 

Re: Jinja2 - Descending order in groupby and dictsort filters

From:
Clodoaldo Neto
Date:
2012-03-23 @ 14:17
Em 23 de março de 2012 09:04, Simon Sapin <simon.sapin@exyr.org> escreveu:

> Le 23/03/2012 12:40, Clodoaldo Neto a écrit : 
> > Is there a way to make either of groupby or dictsort filters to sort in 
> > reverse order? 
> > 
> > The last dictsort example in the doc includes the words "sorted 
> > normally" suggesting there would be a "sorted abnormally" option: 
> > 
> > {%  for  item  in  mydict|dictsort(false,  'value')  %} 
> >      sort the dict by key, case insensitive, sorted 
> >      normally and ordered by value. 
> 
> 
> Hi, 
> 
> The current dictsort filter does not do that: 
> 
> https://github.com/mitsuhiko/jinja2/blob/master/jinja2/filters.py#L187 
> 
> However you could easily add a "reversed" argument (defaults to False) 
> to the filter that would just be passed to sorted() 
> 
> The best would be to send a pull request, but until the feature is 
> merged and released you can override the filter: 
> 
> app.add_template_filter(reversable_dictsort, name='dictsort') 
> 
> 
> > As for groupby there is no mention of sorting order option. 
> 
> Same story. 
> 
> 
I read somewhere that Jinja2's groupby is based on itertools groupby which
does not accept a sort order option. So I guess it would not be trivial, or
is it?. Right now what I'm doing is to sort the groupby output:

{% for group in my_list | groupby('some_field') | sort(reverse=True) %}

Yes I know it is double sorting.

Clodoaldo


> Regards, 
> -- 
> Simon Sapin 
> 

Re: Jinja2 - Descending order in groupby and dictsort filters

From:
Simon Sapin
Date:
2012-03-23 @ 19:30
Le 23/03/2012 15:17, Clodoaldo Neto a écrit :
> I read somewhere that Jinja2's groupby is based on itertools groupby 
> which does not accept a sort order option. So I guess it would not be 
> trivial, or is it?. Right now what I'm doing is to sort the groupby output: 
> 
> {% for group in my_list | groupby('some_field') | sort(reverse=True) %} 
> 
> Yes I know it is double sorting. 

Just read the code:

https://github.com/mitsuhiko/jinja2/blob/master/jinja2/filters.py#L647

Jinja’s groupby is based on itertools.groupby, but it also sorts. These 
sorts could have a "reverse" argument.

Jinja even does a double sort. Although I’m not sure the second sort is 
necessary (is everything not taken care of by the first sort?) 
double-sorting is not so bad in Python due to how the algorithm is 
implemented:

http://wiki.python.org/moin/HowTo/Sorting#Sort_Stability_and_Complex_Sorts

> The Timsort algorithm used in Python does multiple sorts efficiently 
> because it can take advantage of any ordering already present in a 
> dataset. 


--  
Simon Sapin 

Re: Jinja2 - Descending order in groupby and dictsort filters

From:
Jökull Sólberg Auðunsson
Date:
2012-03-25 @ 17:42
django's groupby is much better. About the only thing that is better : )

On Fri, Mar 23, 2012 at 7:30 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 23/03/2012 15:17, Clodoaldo Neto a écrit : 
> > I read somewhere that Jinja2's groupby is based on itertools groupby 
> > which does not accept a sort order option. So I guess it would not be 
> > trivial, or is it?. Right now what I'm doing is to sort the groupby 
> output: 
> > 
> > {% for group in my_list | groupby('some_field') | sort(reverse=True) %} 
> > 
> > Yes I know it is double sorting. 
> 
> Just read the code: 
> 
> https://github.com/mitsuhiko/jinja2/blob/master/jinja2/filters.py#L647 
> 
> Jinja’s groupby is based on itertools.groupby, but it also sorts. These 
> sorts could have a "reverse" argument. 
> 
> Jinja even does a double sort. Although I’m not sure the second sort is 
> necessary (is everything not taken care of by the first sort?) 
> double-sorting is not so bad in Python due to how the algorithm is 
> implemented: 
> 
> http://wiki.python.org/moin/HowTo/Sorting#Sort_Stability_and_Complex_Sorts 
> 
> > The Timsort algorithm used in Python does multiple sorts efficiently 
> > because it can take advantage of any ordering already present in a 
> > dataset. 
> 
> 
> -- 
> Simon Sapin 
>