wxPython, sorting dates in a ListCtrl with the ColumnSorterMixin

J

John Taylor

I have a ListCtrl with 5 columns. The first 4 columns are either
strings or integers, but the last column is a string in the format of
MM-DD-YYYY. I searched google and also read over the
ColumnSorterMixin code & examples, but still can not figure out how to
to write a user-defined sort routine just for the last column in order
to sort that column by date. I still want the sorting of the first
four columns to work as expected. How is this done?

I am using Python 2.3.4 and wxPython 2.5.2.8.

Thanks,
-John
 
R

Remi Villatel

John said:
I have a ListCtrl with 5 columns. The first 4 columns are either
strings or integers, but the last column is a string in the format of
MM-DD-YYYY. I searched google and also read over the
ColumnSorterMixin code & examples, but still can not figure out how to
to write a user-defined sort routine just for the last column in order
to sort that column by date. I still want the sorting of the first
four columns to work as expected. How is this done?

I am using Python 2.3.4 and wxPython 2.5.2.8.

Here is a tiny bit of code to sort dates.

--------------------------------
# Dates must look like YYYY-MM-DD to be sortable.
# I hope your dates all have the same length.
# Otherwise, you'll have to improve the def.
#
def uniformized(dd): return dd[-4:]+dd[:2]+dd[3:5]

# Let's create some dates to sort.
#
entry=["12-31-2020","07-14-1792","10-08-2004","06-01-1920"]
nbr_of_entries=len(entry)

# Now, let's sort!
#
temp=[]

for foo in range(nbr_of_entries):
temp.append( (uniformized(entry[foo]),foo) ) # That's a tuple.

temp.sort()

for ignore,a in temp: # Only the var _a_ matters.
print entry[a]

--------------------------------

In the last loop, _a_ is the key to access all columns which will then be
sorted according to the dates in the fifth column.

In the _foo_ loop, if you replace the uniformized date by the content of any
other column, the whole table will be sorted according to this column when
you'll access it with the _a_ loop.

Of course, your datas remain untouched. Only the _a_ key enables you to
access them as if they were really sorted.

I hope that's clear...

See ya,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top