sorting columns in a grid

394 views
Skip to first unread message

Big_Killers

unread,
Aug 9, 2009, 2:12:41 PM8/9/09
to wxPython-users
The app I am writing loads a csv file into a table, and then into a
grid. I can manage to sort the grid by the first column (see below),
but can't manage to sort it by any other columns. Please could
anybody help?

Robin, if you're on-line, surely 2 mins to work this out for a man of
your immense intellect! Keep up the good work.

Regards.

# use simple csv function to load data into x
x = read_csv_file('c:\data.csv')

# sort function attached to a button
def OnSort(self, event):
x.sort()

# update the display (only way I could get it to work)
self._mgr.GetPane("grid_content").Hide()
self._mgr.Update()
self._mgr.GetPane("grid_content").Show()
self._mgr.Update()
return

Mike Driscoll

unread,
Aug 10, 2009, 10:02:17 AM8/10/09
to wxPython-users


On Aug 9, 1:12 pm, Big_Killers <jamie.kilpatrick...@btinternet.com>
wrote:
There doesn't seem to be a builtin way to do this. You'll have to
manage the data yourself somehow. Probably the easiest way I can think
of off the top of my head is to load your data into an in-memory
sqlite database (assuming it's not huge) and load the data from sqlite
into your grid.

Then you can use SQL calls to sort it and then reload it. I think you
can use the Refresh() command rather than that 5 liner above, but I'm
not sure since that appears to be AUI related.

- Mike

Mauro Cavalcanti

unread,
Aug 10, 2009, 10:17:11 AM8/10/09
to wxPytho...@googlegroups.com
Hi,

In one application I have developed using wxPython and SQLite as
database backend, I implemented sorting of data by any column with the
simple method below:

def SortColumn(self, col):
self.data.sort(lambda x,y: cmp(x[col], y[col]))

This method is attached to a class "DataTable" descending from
wx.grid.PyGridTableBase, which receives the data from the SQLite
database.

You can get the app GPLed source code at http://ecolog.infobio.net

Hope this helps.

Best regards,

2009/8/9 Big_Killers <jamie.kil...@btinternet.com>:
--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: maur...@gmail.com
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."

Robin Dunn

unread,
Aug 10, 2009, 2:27:48 PM8/10/09
to wxPytho...@googlegroups.com
Mauro Cavalcanti wrote:
> Hi,
>
> In one application I have developed using wxPython and SQLite as
> database backend, I implemented sorting of data by any column with the
> simple method below:
>
> def SortColumn(self, col):
> self.data.sort(lambda x,y: cmp(x[col], y[col]))
>
> This method is attached to a class "DataTable" descending from
> wx.grid.PyGridTableBase, which receives the data from the SQLite
> database.

That will also work if self.data is just a list of lists, no need to use
a SQL DB for this unless you need to use if for other things as well.

To wrap all of this up and tie a bow on it, how the data is managed in
the grid table is entirely up to you. You 'sort' it simply by returning
different values when the grid asks for the data for a particular row.
How you manage to do that is dependent on how you are storing the data.
If the table has all the data items in a list then all you need to do
is sort the list. If it is getting them from a DB then you just issue a
new query with an ORDER BY clause. If you have a mapping to some other
data structure elsewhere in the app then just reorder the mapping. Etc.


--
Robin Dunn
Software Craftsman
http://wxPython.org

Reply all
Reply to author
Forward
0 new messages