-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Implement natural sorting for the sites list #303
Comments
Looks like another candidate for Just out of curiosity, why do you name sites like this? |
That would be awesome, I had a read but django isn't really my thing - was hoping to just be able to fix and send back :-) Sites of ours are assigned a number which everything is labelled against - so switches are sw-6-1 sw-6-2 etc, routers bdr-1-1, bdr-1-2, bdr-6-1 - Was done before I started and I just kept it going for new sites. |
My coworkers on the enterprise side name sites(aka offices) like that as well because it matches their ip space assigned. I find it odd, but to each their own. |
I know I suggested natsort earlier, but thinking further on this I'm not sure it's a road we want to go down. Calling A more efficient approach would be to effect sorting the database by CASTing integers, but it's impossible to achieve a foolproof sorting scheme since the format of a site name is arbitrary. Maybe it would be enough to order sites (and potentially other objects) first by a leading integer, if one exists, and then fall back to the default ordering. Something like this:
|
I'll do some research, try and see if i can find a more general solution, possibly chunk the string into up to 3 or 4 parts to sort each chunk normally, which should result in better? returned ordering, and still pre data read on the db. |
I saw this option http://stackoverflow.com/questions/12965463/humanized-or-natural-number-sorting-of-mixed-word-and-number-strings/20667107#20667107 actually implementing something like that seemed to require falling back to a querysetraw which seems to not be worth it at all. Some of the other options seemed to require custom types or tables which could be doable, but not directly obvious from django's ORM. I have here the best i could come up with in those constraints. Given some data from the provided example at the top of the issue here is the sorted results I'll submit a pull request for the complete thing if this is a decent alternative, the bulk of the code is this function.
|
I think we can simplify that a lot. I only see the need for three chunks:
This should cover 99% of cases, I would think. |
I think I have the ordering pretty much nailed down:
Just deciding now how best to integrate it via managers. |
Out of curiosity; why do the sort server-side? This requires extra queries every time you resort a column by clicking on it. As an example; Datatables for jQuery has natural sort. |
Sorting on the client side requires pulling down the entire table. For some objects, this is potentially thousands of rows. In these cases it's usually much faster to cap the database query and return only the subset of rows which will be displayed. |
…or sites, racks, and devices
Orders 10, 11, 12, 13, 1, 2, 3
The text was updated successfully, but these errors were encountered: