#NEWLINE# def python2sort( x , key = None ) : #NEWLINE# #INDENT# if ( len ( x ) == 0 ) : #NEWLINE# #INDENT# return x #NEWLINE# #UNINDENT# it = iter ( x ) #NEWLINE# groups = [ [ next ( it ) ] ] #NEWLINE# for item in it : #NEWLINE# #INDENT# for group in groups : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# item_precedence = ( item if ( key is None ) else key ( item ) ) #NEWLINE# group_precedence = ( group [ 0 ] if ( key is None ) else key ( group [ 0 ] ) ) #NEWLINE# ( item_precedence < group_precedence ) #NEWLINE# group . append ( item ) #NEWLINE# break #NEWLINE# #UNINDENT# except TypeError : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# groups . append ( [ item ] ) #NEWLINE# #UNINDENT# #UNINDENT# return itertools . chain . from_iterable ( ( sorted ( group , key = key ) for group in groups ) )