sorting list for display with python or jquery for cherrypy page


I have a cherrypy project which has a webpage to display the results of a database query as a list of all the rows, sometimes 10,000+ rows. The database query returns a list with each item of the list being a tuple of all the columns from the query. I wanted to put some sort of default sort order to make the pagination I have added more usable. But, as I have tested adding the sort with python and jquery I have found limitations of both that I am unsure if I just don't know how to overcome them or if I should just use the other method.

With my python method I use the sorted() function and the itemgetter operator, but I find that with a mix of letters of varying case and numbers in the column used for sorting it uses ASCIIbetical order [0, 10, 2, 20, A, C, Z, a, z]. This sort order is not ideal, but I have not been able to find a way yet to manipulate the sort to a different type. An example of the code I would use to sort is below

from operator import itemgetter
random_list = [('a', 'value'), ('b', 'value'), ('0', 'value'), ('8', 'value'), ('Z', 'value'), ('2', 'value'), ('A', 'value'), ('z', 'value'), ('100', 'value'), ('250', 'value'), ('20', 'value'), ('25', 'value'), ('10', 'value'), ('11', 'value'), ('c', 'value')]
sorted_results = sorted(random_list,key=itemgetter(0))
print sorted_results
[('0', 'value'), ('10', 'value'), ('100', 'value'), ('11', 'value'), ('2', 'value'), ('20', 'value'), ('25', 'value'), ('250', 'value'), ('8', 'value'), ('A', 'value'), ('Z', 'value'), ('a', 'value'), ('b', 'value'), ('c', 'value'), ('z', 'value')]

This method currently takes about 6-7 seconds less to load the page fully than the jquery method I will describe below.

For jquery sorting I have been trying out the tablesorter jquery plugin. I included the jquery.tablesorter.js file in my project and linked to it on my page. This method returns alphabetical order and I instead see a list order like [0, A, C, Z, a, z, 2, 10, 20] which is much better than the ASCIIbetical from my python method. This jquery method though does take much longer to sort the results and have a fully loaded/functional page.

Below is a snip of my jquery code linked to my html page with the table to be sorted with id="data-table"

$( document ).ready(function() {
    .tablesorter({sortList: [[1,0],[0,0]]});

Is there a way to manipulate the quicker python method to instead sort how I would like, or are there methods I should look into for optimizing how the jquery works?


randomir in the comments pointed me to the natsort module for python and it appears to be exactly the type of sorting I was looking for that is quicker than my jquery method. If anyone else has other suggestions, I am always open to optimization

Show source
| sorting   | python   | jquery   | tablesorter   | cherrypy   2017-08-08 22:08 0 Answers

Answers to sorting list for display with python or jquery for cherrypy page ( 0 )

Leave a reply to - sorting list for display with python or jquery for cherrypy page

◀ Go back