Skip to content

Sorting in advanced way #30

@lee1043

Description

@lee1043

Replacing python's default sorting to more human recognizable way.

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

Example:

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

Further detail: PCMDI/click#10

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureCategorizes issue as related to feature request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions