-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathsortarray.py
163 lines (135 loc) · 5.45 KB
/
sortarray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# !/usr/local/bin/python3.6
# MIT licensed
# Copyright (c) 2018 White Russsian
# Github: <https://github.com/Eve-PySpy/PySpy>**********************
'''SortArray() provides functionality to sort an array (list of lists
or tuples) containing string or numeric values (or a mix thereof).
Ability to sort by two "columns", to break a tie in the primary sort
column. Can be case sensitive.
'''
# cSpell Checker - Correct Words****************************************
# // cSpell:words russsian
# **********************************************************************
class SortArrayError(Exception):
'''
Base exception for any exceptions raised by SortArray.
'''
def __init__(self, msg=None):
if msg is None:
# Set some default useful error message
msg = "SortArray encountered a problem and stopped."
super(SortArrayError, self).__init__(msg)
class OutOfBoundError(SortArrayError):
'''
Gets raised when `prim_col` is greater than the last column in `array`.
'''
def __init__(self, prim_col, sort_array):
prim_col = prim_col
array_columns = len(sort_array[0])
msg = (
"Sort column index (%s) exceeds number of columns in the unsorted input array (%s)." % (prim_col, array_columns)
)
super(SortArrayError, self).__init__(msg)
def _determineApproach(array, sort_col):
'''
Takes an array (list of lists/tuples) and determines whether values
in a given column should be sorted as numbers or strings. Returns
a boolean which is True if column should be sorted as string and
False if to be sorted as float.
:param `array`: A list of list or tuples.
:param `sort_col`: The number of the sort column as integer.
:return: Boolean indicating if sort type is string (True) or float
(False).
'''
if sort_col > len(array[0]):
raise OutOfBoundError(sort_col, array)
# Check type of each element to be sorted_list
is_num = is_str = is_other = is_none = 0
for r in array:
if isinstance(r[sort_col], (int, float)):
is_num += 1
elif isinstance(r[sort_col], str):
try:
float(r[sort_col])
is_num += 1
except ValueError:
is_str += 1
elif r[sort_col] is None:
is_none += 1
else:
is_other += 1
# Make sure all elements are sortable, if not return TypeError
if is_other > 0:
raise TypeError
# If any element is not or cannot be converted to a number,
# treat all elements as string.
elif is_str > 0:
sort_as_str = True
else:
sort_as_str = False
return sort_as_str
def sort_array(array, prim_col, sec_col=None, prim_desc=False, sec_desc=False, case_sensitive=False):
'''
Take an array (list of lists/tuples) with numeric or text values (or
mix of both) and sort it by a primary column and optionally by a
secondary column. It is tollerant to None values.
:param `array`: List of lists or tuples containing strings
or numbers.
:param `prim_col`: Index (integer) of primary sort column.
:param `sec_col`: Index (integer) of secondary sort column.
:param `prim_desc`: Boolean indicating sort order of primary sort
column(`True` for descending `False` for ascending)
:param `sec_desc`: Boolean indicating sort order of secondary sort
column(`True` for descending `False` for ascending)
:param `case_sensitive`: Boolean set to True if cases matters.
:return: The sorted array as list of tuples.
'''
# Determine if we need to convert values to str or float
prim_is_str = _determineApproach(array, prim_col)
if sec_col is not None and sec_col != prim_col:
sec_is_str = _determineApproach(array, sec_col)
else:
sec_col = None
# We mke use of the fact that sorted() is stable and first sort by
# the secondary sort column, if any, and then by the primary.
# Secondary Sort --------------------------------------------------
if sec_col is not None:
if sec_is_str and case_sensitive:
sorted_array = sorted(
array,
key=lambda r: str(r[sec_col]) if r[sec_col] is not None else "",
reverse=sec_desc
)
elif sec_is_str and not case_sensitive:
sorted_array = sorted(
array,
key=lambda r: str(r[sec_col]).lower() if r[sec_col] is not None else "",
reverse=sec_desc
)
else:
sorted_array = sorted(
array,
key=lambda r: float(r[sec_col]) if r[sec_col] is not None else float("-inf"),
reverse=sec_desc
)
array = sorted_array
# Primary Sort -----------------------------------------------------
if prim_is_str and case_sensitive:
sorted_array = sorted(
array,
key=lambda r: str(r[prim_col]) if r[prim_col] is not None else "",
reverse=prim_desc
)
elif prim_is_str and not case_sensitive:
sorted_array = sorted(
array,
key=lambda r: str(r[prim_col]).lower() if r[prim_col] is not None else "",
reverse=prim_desc
)
else:
sorted_array = sorted(
array,
key=lambda r: float(r[prim_col]) if r[prim_col] is not None else float("-inf"),
reverse=prim_desc
)
return sorted_array