mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 19:43:41 +02:00
Fix/sort names (#177)
* cosmetic fix in sort_names * bug fix, more tests
This commit is contained in:
parent
15d07de87f
commit
5155effbbf
2 changed files with 37 additions and 12 deletions
|
@ -18,30 +18,32 @@ def sort_names(ll):
|
|||
sorted list
|
||||
"""
|
||||
if len(ll) > 1:
|
||||
sorted = False
|
||||
r_pattern = r'r(\d+)'
|
||||
id_pattern = r'id(\d+)'
|
||||
|
||||
# sort list by id first
|
||||
if all([re.search(id_pattern, entry) for entry in ll]):
|
||||
ll.sort(key=lambda x: int(re.findall(id_pattern, x)[0]))
|
||||
sorted = True
|
||||
# then by replikum
|
||||
if all([re.search(r_pattern, entry) for entry in ll]):
|
||||
ll.sort(key=lambda x: int(re.findall(r_pattern, x)[0]))
|
||||
sorted = True
|
||||
# as the rearrangements by one key let the other key untouched, the list is sorted now
|
||||
|
||||
else:
|
||||
if not sorted:
|
||||
# fallback
|
||||
sames = ''
|
||||
if len(ll) > 1:
|
||||
for i in range(len(ll[0])):
|
||||
checking = ll[0][i]
|
||||
for rn in ll[1:]:
|
||||
is_same = (rn[i] == checking)
|
||||
if is_same:
|
||||
sames += checking
|
||||
else:
|
||||
break
|
||||
print("Using prefix:", ll[0][len(sames):])
|
||||
for i in range(len(ll[0])):
|
||||
checking = ll[0][i]
|
||||
for rn in ll[1:]:
|
||||
is_same = (rn[i] == checking)
|
||||
if is_same:
|
||||
sames += checking
|
||||
else:
|
||||
break
|
||||
print("Using prefix:", sames)
|
||||
ll.sort(key=lambda x: int(re.findall(r'\d+', x[len(sames):])[0]))
|
||||
return ll
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue