Skip to content
This repository has been archived by the owner on Feb 9, 2023. It is now read-only.

Sorting in advanced way #10

Closed
lee1043 opened this issue May 14, 2019 · 2 comments
Closed

Sorting in advanced way #10

lee1043 opened this issue May 14, 2019 · 2 comments

Comments

@lee1043
Copy link
Contributor

lee1043 commented May 14, 2019

In the interactive portrait plot, would it be available to add below function replacing the python’s default sorting?

import copy
import re

def sort_human(input_list):
    l = copy.copy(input_list)
    convert = lambda text: float(text) if text.isdigit() else text
    alphanum = lambda key: [ convert(c) for c in re.split('([-+]?[0-9]*\.?[0-9]*)', key) ]
    l.sort( key=alphanum )
    return l

Above function is what that I have used for creating my own portrait plot before your modal capability has become available. It sorts labels more in recognizable way.

For example, default sorted sorts as below:
A_r10i1p1
A_r1i1p1
A_r20i1p1
A_r2i1p1
A_r3i1p1

And the function I implemented sorts as below:
A_r1i1p1
A_r2i1p1
A_r3i1p1
A_r10i1p1
A_r20i1p1

I guess the right place might be in click/click_plots/portrait_plots.py, line 174 and 176 where sorted has been used, but would be nicer if you could confirm this for me.

Below is for actual use example.

>>> a=['a_1', 'a_11', 'a_2']
>>> sorted(a)
['a_1', 'a_11', 'a_2']

>>> import copy
>>> import re
>>> def sort_human(input_list):
...     l = copy.copy(input_list)
...     convert = lambda text: float(text) if text.isdigit() else text
...     alphanum = lambda key: [ convert(c) for c in re.split('([-+]?[0-9]*\.?[0-9]*)', key) ]
...     l.sort( key=alphanum )
...     return l
>>> sort_human(a)
['a_1', 'a_2', 'a_11']
>>> 
@durack1
Copy link

durack1 commented May 14, 2019

@lee1043 thanks for this, I have always disliked the python default sorting, the example above is certainly more preferable - it'd be a great function to have in a general library

@doutriaux1
Copy link
Contributor

@durack1 agreed. @lee1043 I'll add it in click plots for sure, would you mind linking to this issue from genutil please. I think as @durack1 mentioned that's where it belongs.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants