I am using below code to sort the treeview column but it is case sensitive i.e. if the column value is [a,c,A]
the sorted value will be [a,c,A]
but i want it as [a,A,c]
and even the number sorting is only based on first digit i.e. if [2,1,11]
is the column value, the output will be [1,11,2]
what i want it to be as [1,2,11]
. please help me with the idea to solve this issue. Thank you
JavaScript
x
17
17
1
for col in cols:
2
self.treeview.heading(col, text=col,command=lambda c=col: self.sortby(self.treeview, c, 0))
3
4
def sortby(self,tree, col, descending):
5
6
data = [(tree.set(child, col), child)
7
for child in tree.get_children('')]
8
9
data.sort(reverse=descending)
10
11
for ix, item in enumerate(data):
12
tree.move(item[1], '', ix)
13
14
tree.heading(col, command=lambda col=col: self.sortby(tree, col,
15
int(not descending)))
16
17
Advertisement
Answer
Case insensitive sorting is done by providing a key mapping function to sort. For example, sort(values, key=str.casefold)
(Python 3).
In the case of sorting over the first value of the tuple, the data sort line would become data.sort(reverse=descending, key=lambda t: str.casefold(t[0]))