0

I have got a list i.e.

ls= [u'Cancer',u"Men's",u'Orthopedics',u'Pediatric',u"Senior's",u"Women's"]

ls.sort() does not seem to work here due to presence of single quote in the list elements.

I need to sort this list. Any idea???

4
  • 4
    Your list is already sorted, and ls.sort() simply leaves it unchanged, as expected. What exactly is your problem? Commented Feb 12, 2012 at 11:00
  • 1
    Please explain what you mean by does not seem to work: if it's not the order that you expect, show us what order you got! Commented Feb 12, 2012 at 11:01
  • 2
    One common pitfall with list.sort" for beginners is that it does not return anything (actually, returns "None") - so, doing sortedlist = mylist.sort() does not work in any case. Use the builtin sorted if you need a copy of the list, like in: sortedlist = sorted(mylist) Commented Feb 12, 2012 at 11:12
  • 2
    Sorry mate I must be drunk... I need to remove this question. both of you are correct Commented Feb 12, 2012 at 11:13

2 Answers 2

5

Actually, the question is valid and the answer is not exactly correct in general case. If the test material was not already sorted, it would not get correctly alphabetized but the 's would cause the list to be sorted to wrong order:

>>> l = ["'''b", "a", "a'ab", "aaa"]
>>> l.sort()
>>> l
["'''b", 'a', "a'ab", 'aaa']
>>> from functools import partial
>>> import string
>>> keyfunc = partial(string.replace, old="'", new="")
>>> l.sort(key=keyfunc)
>>> l
['a', 'aaa', "a'ab", "'''b"]
Sign up to request clarification or add additional context in comments.

3 Comments

If you refer to my answer as not being exactly correct, could you please clarify what exactly isn't correct?
kimvais is right..his explanation is more accurate..thanks man
@user1170364: Please reconsider. This answer says "the 's would cause the list to be sorted to wrong order", which simply is not true. The truth is that ' is sorted before all alphanumeric characters, which isn't "wrong", but might not be what you want in some given context.
3
>>> ls
[u'Cancer', u"Men's", u'Orthopedics', u'Pediatric', u"Senior's", u"Women's"]
>>> ls.sort()
>>> ls
[u'Cancer', u"Men's", u'Orthopedics', u'Pediatric', u"Senior's", u"Women's"]

Since the list was sorted in the first place, it didn't change. sort has no problem with ' - but note that it sorts before the a-z and A-Z characters:

>>> ls
[u'abc', u'abz', u"ab'"]
>>> ls.sort()
>>> ls
[u"ab'", u'abc', u'abz']
>>> 

4 Comments

You can ignore case in sorting by import string and ls.sort(key=string.lower)
This is correct in this exact case, but not generally. @EliBendersky see my example, for example ["a'b", "aa"] won't get correctly alphabetically sorted without a key function.
@Kimvais: I truly don't understand what you mean, but hey, if the OP found in your answer what he needed, I don't mind! Good for him.
what I mean is that aa'b being before aaa is not in alphabetically correct order

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.