Export to GitHub

gui2py - issue #14

ListView sorting of numbers


Posted on Sep 15, 2014 by Swift Bird

What steps will reproduce the problem? 1. Set up a gui.ListView with at least 1 gui.ListColumn 2. Populate the gui.ListColumn with numbers (must be strings) 3. Run your gui code 4. Sort the list by clicking on the col name in the gui

What is the expected output? What do you see instead? I want the numbers to be sorted in correct ascending or descending order. Currently they are sorted as such: 1, 11, 2, 2345, 3.

What version of the product are you using? On what operating system? Current version from github (https://github.com/reingart/gui2py/commit/b5a37bc7a8e6aa88e8a38acae7e49f0f07dd1168)

Please provide any additional information below. I've attached a file showing screenshots of the current results. I've clicked on the col name "Laps".

Attachments

Comment #1

Posted on Sep 15, 2014 by Swift Bird

I am using wxPython3.0-win32-py27 on a windows 7 machine.

Comment #2

Posted on Sep 15, 2014 by Swift Bird

I realized that I could work around this by hacking together some string formatting which grabs the decimal, floors the number, rjust to right align, merge the round number back with the decimal and then insert into the listview.

Feels dirty:

def format_listitem(number,dec=1): """ Accepts a number (either integer or floating point) and returns a formatted string for use in listviews """

rounded = int(math.floor(float(number)))
if dec == 1:
    decimal = "{0:.1f}".format(float(number) - float(rounded))
elif dec == 2:
    decimal = "{0:.2f}".format(float(number) - float(rounded))
formatted_string = "%s.%s" % (str(rounded).rjust(3), str(decimal))
formatted_string = formatted_string.replace(".0.",".")  

return formatted_string

Comment #3

Posted on Sep 15, 2014 by Swift Bird

However, rjust trickery can't fix sorting issues if the list contains positive and negative numbers :(

Comment #4

Posted on Sep 15, 2014 by Swift Bird

I figure that if I wasn't using gui2py I could set up a ColumnSorterMixin that made use of something like this:

def natural_keys(text): ''' alist.sort(key=natural_keys) sorts in human order http://nedbatchelder.com/blog/200712/human_sorting.html '''
return [ atoi(c) for c in re.split('([-]?\d+)', text) ]

Status: New

Labels:
Type-Defect Priority-Medium