'Sorting tree wrt top level items'

def sort_top_level_items(self, key):

    self.save_expanded_state()
    items = sorted([self.takeTopLevelItem(0) for index in range(self.topLevelItemCount())], key=key)
    for (index, item) in enumerate(items):
        self.insertTopLevelItem(index, item)
    self.restore_expanded_state()
