Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
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?
return ''.join(sorted(comb_))
Add a comment
def sortstr(comb_): return ''.join(sorted(comb_))
e:f;b :(
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)
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)
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.