reingart / gui2py_googlecode

Automatically exported from code.google.com/p/gui2py
GNU Lesser General Public License v3.0
0 stars 1 forks source link

ListView sorting of numbers #14

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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/b5a37bc7a8e6aa88e8a38acae7e49f0f07dd1
168)

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".

Original issue reported on code.google.com by rob.cro...@gmail.com on 15 Sep 2014 at 4:38

Attachments:

GoogleCodeExporter commented 9 years ago
I am using wxPython3.0-win32-py27 on a windows 7 machine.

Original comment by rob.cro...@gmail.com on 15 Sep 2014 at 4:45

GoogleCodeExporter commented 9 years ago
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

Original comment by rob.cro...@gmail.com on 15 Sep 2014 at 5:44

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

Original comment by rob.cro...@gmail.com on 15 Sep 2014 at 6:36

GoogleCodeExporter commented 9 years ago
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) ]

Original comment by rob.cro...@gmail.com on 15 Sep 2014 at 7:06