Revision 727
Added by Aaron Marcuse-Kubitza almost 13 years ago
sort | ||
---|---|---|
6 | 6 |
|
7 | 7 |
def by_idx(list_, idxes): return [list_[i] for i in idxes] |
8 | 8 |
|
9 |
def cmp_nulls_last(*row_pair): |
|
10 |
def repl(val): |
|
11 |
if val == '': val = () # sorts > '' |
|
12 |
return val |
|
13 |
return cmp(*map(lambda row: map(repl, row), row_pair)) |
|
14 |
|
|
9 | 15 |
def main(): |
10 | 16 |
col_nums = sys.argv[1:] |
11 | 17 |
if col_nums == []: |
... | ... | |
17 | 23 |
cols = reader.next() |
18 | 24 |
def get_sort_by(row): return by_idx(row, col_nums) |
19 | 25 |
rows = list(reader) |
20 |
rows.sort(lambda *rows: cmp(*map(get_sort_by, rows)))
|
|
26 |
rows.sort(lambda *row_pair: cmp_nulls_last(*map(get_sort_by, row_pair)))
|
|
21 | 27 |
writer = csv.writer(sys.stdout) |
22 | 28 |
writer.writerow(cols) |
23 | 29 |
map(writer.writerow, rows) |
Also available in: Unified diff
sort: Sort empty strings last so that inputs with no mapping go at the end of the map spreadsheet