def sortby(tree, col, descending):
 data = [(tree.set(child, col), child) for child in tree.get_children('')]
 data.sort(reverse=descending)
 for (indx, item) in enumerate(data):
  tree.move(item[1], '', indx)
 tree.heading(col, command=(lambda col=col: sortby(tree, col, int((not descending)))))

def sortby ( tree , col , descending ) : data = [ ( tree . set ( child , col ) , child ) for child in tree . get_children ( '' ) ] data . sort ( reverse = descending ) for ( indx , item ) in enumerate ( data ) : tree . move ( item [ 1 ] , '' , indx ) tree . heading ( col , command = ( lambda col = col : sortby ( tree , col , int ( ( not descending ) ) ) ) )
sort tree contents when a column is clicked on .