case-insensitive treeview sorting

Tags: , , ,



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

for col in cols:
            self.treeview.heading(col, text=col,command=lambda c=col: self.sortby(self.treeview, c, 0))

def sortby(self,tree, col, descending):

            data = [(tree.set(child, col), child) 
            for child in tree.get_children('')]

            data.sort(reverse=descending)

            for ix, item in enumerate(data):
                tree.move(item[1], '', ix)

            tree.heading(col, command=lambda col=col: self.sortby(tree, col, 
                int(not descending)))
            

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]))



Source: stackoverflow