nodes: 0, module; 1, function_definition; 2, function_name:sort_top_level_items; 3, parameters; 4, block; 5, identifier:self; 6, identifier:key; 7, expression_statement; 8, expression_statement; 9, expression_statement; 10, for_statement; 11, expression_statement; 12, comment:"""Sorting tree wrt top level items"""; 13, call; 14, assignment; 15, pattern_list; 16, call; 17, block; 18, call; 19, attribute; 20, argument_list; 21, identifier:items; 22, call; 23, identifier:index; 24, identifier:item; 25, identifier:enumerate; 26, argument_list; 27, expression_statement; 28, attribute; 29, argument_list; 30, identifier:self; 31, identifier:save_expanded_state; 32, identifier:sorted; 33, argument_list; 34, identifier:items; 35, call; 36, identifier:self; 37, identifier:restore_expanded_state; 38, list_comprehension; 39, keyword_argument; 40, attribute; 41, argument_list; 42, call; 43, for_in_clause; 44, identifier:key; 45, identifier:key; 46, identifier:self; 47, identifier:insertTopLevelItem; 48, identifier:index; 49, identifier:item; 50, attribute; 51, argument_list; 52, identifier:index; 53, call; 54, identifier:self; 55, identifier:takeTopLevelItem; 56, integer:0; 57, identifier:range; 58, argument_list; 59, call; 60, attribute; 61, argument_list; 62, identifier:self; 63, identifier:topLevelItemCount
code: def sort_top_level_items(self, key):
          """Sorting tree wrt top level items"""
          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()