diff options
author | BonfaceKilz | 2020-10-02 01:04:23 +0300 |
---|---|---|
committer | BonfaceKilz | 2020-10-02 01:04:23 +0300 |
commit | 5aee7c35ef27dbfa590dfd30364000840dd0006a (patch) | |
tree | 0e45da1e135181d34027a761389cdeba444f34f0 | |
parent | 509bd3b90dcb6ab71f42385d1feccb154a6d5108 (diff) | |
download | genenetwork2-5aee7c35ef27dbfa590dfd30364000840dd0006a.tar.gz |
Update natural_sort function
* wqflask/wqflask/show_trait/SampleList.py
(natural_sort): Add new function.
(SampleList.get_attributes): Use new sort function to do natural_sort.
-rw-r--r-- | wqflask/wqflask/show_trait/SampleList.py | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/wqflask/wqflask/show_trait/SampleList.py b/wqflask/wqflask/show_trait/SampleList.py index 7a6f1664..b43b57dc 100644 --- a/wqflask/wqflask/show_trait/SampleList.py +++ b/wqflask/wqflask/show_trait/SampleList.py @@ -1,3 +1,4 @@ +import re from flask import Flask, g from base import webqtlCaseData @@ -105,8 +106,7 @@ class SampleList(object): self.attributes[key] = Bunch() self.attributes[key].name = name self.attributes[key].distinct_values = [item.Value for item in values] - self.attributes[key].distinct_values.sort(key=natural_sort_key) - + natural_sort(self.attributes[key].distinct_values) all_numbers = True for value in self.attributes[key].distinct_values: try: @@ -152,11 +152,15 @@ class SampleList(object): """Returns true if SE values exist for any samples, otherwise false""" return any(sample.variance for sample in self.sample_list) -def natural_sort_key(x): - """Get expected results when using as a key for sort - ints or strings are sorted properly""" - - try: - x = int(x) - except ValueError: - pass - return x + + +def natural_sort(list, key=lambda s:s): + """ + Sort the list into natural alphanumeric order. + """ + def get_alphanum_key_func(key): + convert = lambda text: int(text) if text.isdigit() else text + return lambda s: [convert(c) for c in re.split('([0-9]+)', key(s))] + sort_key = get_alphanum_key_func(key) + list.sort(key=sort_key) + |