9

I need to sort 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?

4 Answers 4

22
return ''.join(sorted(comb_)) 
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer. It cannot get more succinct (or idiomatic) than that. Disregarding the strange underscore in the variable name, of course.
3
def sortstr(comb_): return ''.join(sorted(comb_)) 

e:f;b :(

Comments

2

If you want to get a string back do this:

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

But if you want a list back, do this:

def sort_string(string): return sorted(string) 

Comments

1
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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.