Sort items in a row using Python

I need to sort the string, and I came up with the following function.

 def mysort (comb_): 
     str = [] 
     size = len (comb_) 
     for c in comb_: 
         str.append (c) 
     str.sort () 
     return '' .join (str) 

Is there any way to make it compact?

+7
python sorting
source share
4 answers
return ''.join(sorted(comb_)) 
+19
source share
 def sortstr(comb_): return ''.join(sorted(comb_)) 

e: f; b: (

+3
source share

If you want to get the string back, follow these steps:

 def sort_string(string): return "".join(sorted(string)) 

But if you want to return the list, do the following:

 def sort_string(string): return sorted(string) 
+2
source share
 def sort_string(s): def sort_string_to_a_list(s): return sorted(s, lambda x,y: cmp(x.lower(), y.lower()) or cmp(x,y)) sorted_list = sort_string_to_a_list(s) return ''.join(sorted_list) 
+1
source share

All Articles