Skip to content

Commit 83b12e6

Browse files
committed
Smart Sort Serology by comparing the numeric digits
1 parent 26f9db7 commit 83b12e6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

pyard/smart_sort.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
expr_regex = re.compile("[PNQLSGg]")
3030
glstring_chars = re.compile("[/|+^~]")
31+
serology_splitter = re.compile(r"(\D+)(\d+)")
3132

3233

3334
@functools.lru_cache(maxsize=constants.DEFAULT_CACHE_SIZE)
@@ -63,7 +64,11 @@ def smart_sort_comparator(a1, a2):
6364

6465
# Handle serology
6566
if ":" not in a1:
66-
return 1 if a1 > a2 else -1
67+
serology1_match = serology_splitter.match(a1)
68+
serology1_num = int(serology1_match.group(2))
69+
serology2_match = serology_splitter.match(a2)
70+
serology2_num = int(serology2_match.group(2))
71+
return 1 if serology1_num > serology2_num else -1
6772

6873
# Extract and Compare 1st fields first
6974
a1_f1 = int(a1[a1.find("*") + 1 : a1.find(":")])

tests/test_smart_sort.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ def test_fourth_field_comparator_ge(self):
104104
allele2 = "HLA-A*01:01:01:09"
105105
self.assertEqual(smart_sort_comparator(allele1, allele2), 1)
106106

107+
def test_serology_le(self):
108+
serology1 = "Cw10"
109+
serology2 = "Cw3"
110+
self.assertEqual(smart_sort_comparator(serology1, serology2), 1)
111+
107112

108113
if __name__ == "__main__":
109114
unittest.main()

0 commit comments

Comments
 (0)