forked from RunaCapital/awesome-oss-alternatives
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort.py
25 lines (15 loc) · 694 Bytes
/
sort.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
def sort_readme():
with open('README.md', 'r', encoding='utf-8') as f:
all = f.readlines()
table_start = '|Category|Company|Description|GitHub Stars|Alternative to|\n'
idx = all.index(table_start)
find_name = lambda x: x[x.index('[') + 1 : x.index(']')].strip()
find_cat = lambda x: x[:x.index('|')].strip()
pairs = [(find_cat(x), find_name(x)) for x in all[idx + 2: -1]]
sorted_pairs = sorted(pairs)
right_elements = [all[idx + 2: -1][pairs.index(i)] for i in sorted_pairs]
all[idx + 2: -1] = right_elements
with open('README.md', 'w', encoding='utf-8') as f:
f.writelines(all)
if __name__ == "__main__":
sort_readme()