Opened 9 years ago

Closed 3 years ago

#6904 closed New feature (wontfix)

Dictsort filter should sort case insensitive

Reported by: michael@… Owned by: nobody
Component: Template system Version: master
Severity: Normal Keywords:
Cc: mpjung@…, bmispelon@… Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Dictsort filter currently sorts case sensitive and thus creates sometimes two groups depending on the case of the text being sorted. The filter should use

.sort(key=str.lower)

Attachments (3)

dictsort.patch (805 bytes) - added by mathwizard 9 years ago.
Patch against current svn patching dictsort and dictsortreversed filters
6904_revision7393.diff (877 bytes) - added by Michael Axiak 9 years ago.
New patch that works in python 2.3
6904_revision7393_better.diff (2.5 KB) - added by Michael Axiak 9 years ago.
Better patch…

Download all attachments as: .zip

Change History (17)

comment:1 Changed 9 years ago by anonymous

Needs documentation: unset
Needs tests: unset
Owner: changed from nobody to anonymous
Patch needs improvement: unset
Status: newassigned

the dictsort function should be like this:

def dictsort(value, arg):
    var_resolve = Variable(arg).resolve
    decorated = [(var_resolve(item), item) for item in value]
    import operator
    decorated.sort(key=lambda x: unicode.lower(x[0]))
    return [item[1] for item in decorated]
dictsort.is_safe = False

comment:2 Changed 9 years ago by anonymous

Has patch: set

comment:3 Changed 9 years ago by mathwizard

Owner: changed from anonymous to mathwizard
Status: assignednew

Changed 9 years ago by mathwizard

Attachment: dictsort.patch added

Patch against current svn patching dictsort and dictsortreversed filters

comment:4 Changed 9 years ago by Michael Axiak

Needs documentation: set
Owner: changed from mathwizard to Michael Axiak
Status: newassigned
Triage Stage: UnreviewedDesign decision needed

The current patch does not work in python 2.3 and unnecessarily imports operator. I'm uploading a new patch to address those issues.

Also, I'm not sure the benefits of this patch outweigh the backwards incompatible nature of this patch. I'm making it design decision needed until a core dev weighs in on it.

Changed 9 years ago by Michael Axiak

Attachment: 6904_revision7393.diff added

New patch that works in python 2.3

comment:5 Changed 9 years ago by Michael Axiak

Whoops...there are two things that are needed for this patch:

  1. Tests
  2. For it to work sanely for non-string objects (there's no guarantee that they are strings).

I am attaching a patch that addresses these two issues.

Changed 9 years ago by Michael Axiak

Better patch...

comment:6 Changed 9 years ago by Michael Axiak

Owner: changed from Michael Axiak to nobody
Status: assignednew

comment:7 Changed 9 years ago by mathwizard

Thanks for vastly improving the patch. I think this feature is good way to go, cause usually when you are searching for something on a page, you don't expect, that items beginning with small letter 'a' will be somewhere down the list instead of being at the top. And the filter IS used to sort things that are being displayed on a page (it's a template filter). So from the perspective of the user this is to good way to go.

comment:8 in reply to:  7 Changed 7 years ago by Waylan Limberg

Replying to mathwizard:

you don't expect, that items beginning with small letter 'a' will be somewhere down the list instead of being at the top.

Not necessarily. For example *nix file systems are case sensitive and if I am displaying a dict of files and directories (simulating a desktop file list), then they should be sorted case sensitive - the way the filter works now.

The point is, there is a use case for both types of sorting. Perhaps a new separate filter should be created to do case incentive sorting or a switch could be provided for the existing filter to turn case-insensitive sorting on and off. However, changing the default behavior of the existing filter is a bad idea. It could create some unexpected (an undesirable) results for existing sites when they update to the latest version of Django.

comment:9 Changed 7 years ago by Michael P. Jung

Cc: mpjung@… added
Patch needs improvement: set

Please don't just lower() the strings when sorting them. Use the collation of the locale.

http://groups.google.com/group/django-developers/browse_thread/thread/a6fa8df5787b9c32

comment:10 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

I'd call this a bug, from a user perspective. Potentially backwards incompatible but probably not a big deal.

comment:11 Changed 5 years ago by anonymous

Easy pickings: unset
UI/UX: unset

comment:12 Changed 5 years ago by Carl Meyer

Triage Stage: Design decision neededAccepted
Type: BugNew feature

I don't think case-sensitive ordering is so obviously wrong in all cases that we can just change the behavior backwards-incompatibly and force anyone who wants the current behavior to implement their own filter.

Accepting this on the basis that it would be reasonable to add something like "idictsort", since case-insensitive would frequently be preferable for strings. It would also be worth looking in more detail at whether there are any downsides to using locale collation for this new tag rather than simple .lower(), as Michael Jung suggests.

comment:13 Changed 3 years ago by Baptiste Mispelon

Cc: bmispelon@… added

For what it's worth, it's already possible to do dictsort:'foo.lower' so I'm -1 on introducing a new tag.

comment:14 Changed 3 years ago by Tim Graham

Resolution: wontfix
Status: newclosed

Agreed with bmispelon. If there's a case where foo.lower won't work, please re-open.

Note: See TracTickets for help on using tickets.
Back to Top