def number_aware_alphabetical_cmp(str1, str2):
 def flatten_tokens(tokens):
  l = []
  for token in tokens:
   if isinstance(token, str):
    for char in token:
     l.append(char)
   else:
    assert isinstance(token, float)
    l.append(token)
  return l
 seq1 = flatten_tokens(tokenize_by_number(str1))
 seq2 = flatten_tokens(tokenize_by_number(str2))
 l = min(len(seq1), len(seq2))
 i = 0
 while (i < l):
  if (isinstance(seq1[i], float) and isinstance(seq2[i], string_types)):
   return (-1)
  elif (isinstance(seq1[i], string_types) and isinstance(seq2[i], float)):
   return 1
  elif (seq1[i] < seq2[i]):
   return (-1)
  elif (seq1[i] > seq2[i]):
   return 1
  i += 1
 if (len(seq1) < len(seq2)):
  return (-1)
 elif (len(seq1) > len(seq2)):
  return 1
 return 0

def number_aware_alphabetical_cmp ( str1 , str2 ) : def flatten_tokens ( tokens ) : l = [ ] for token in tokens : if isinstance ( token , str ) : for char in token : l . append ( char ) else : assert isinstance ( token , float ) l . append ( token ) return l seq1 = flatten_tokens ( tokenize_by_number ( str1 ) ) seq2 = flatten_tokens ( tokenize_by_number ( str2 ) ) l = min ( len ( seq1 ) , len ( seq2 ) ) i = 0 while ( i < l ) : if ( isinstance ( seq1 [ i ] , float ) and isinstance ( seq2 [ i ] , string_types ) ) : return ( - 1 ) elif ( isinstance ( seq1 [ i ] , string_types ) and isinstance ( seq2 [ i ] , float ) ) : return 1 elif ( seq1 [ i ] < seq2 [ i ] ) : return ( - 1 ) elif ( seq1 [ i ] > seq2 [ i ] ) : return 1 i += 1 if ( len ( seq1 ) < len ( seq2 ) ) : return ( - 1 ) elif ( len ( seq1 ) > len ( seq2 ) ) : return 1 return 0
cmp function for sorting a list of strings by alphabetical order .