0

I need to sort a list of words based on two criteria given. I need to return a list with the same words in order of length (longest to shortest) and the second sort criteria should be alphabetical.

Example list :

l = ['aa','aaa','aaaa','b','bb','z','ccc']

Desired output:

['aaaa', 'aaa', 'ccc', 'aa', 'bb', 'b', 'z']
Ruli
  • 2,592
  • 12
  • 30
  • 40
John
  • 33
  • 1
  • 1
  • 2
  • 8
    Is this homework? If not, where is it from? What have you tried? You need to give more details or people won't be very willing to help, especially if we think that you're blatantly trying to get us to do your assignments for you. – templatetypedef Jan 11 '11 at 08:31
  • 3
    http://stackoverflow.com/questions/4659524/how-to-sort-by-length-of-string-followed-by-alphabetical-order - others have tried more... – eumiro Jan 11 '11 at 15:57
  • 1
    Does this answer your question? [how to sort by length of string followed by alphabetical order?](https://stackoverflow.com/questions/4659524/how-to-sort-by-length-of-string-followed-by-alphabetical-order) – Jongware May 01 '20 at 16:47

5 Answers5

10

You only need one call to sort, because Python automatically sorts tuples lexicographically. That is, if you ask Python to compare two tuples it will order them by their first element, except if those compare equal in which case it will order them by their second element, except if those compare equal in which case...

You want to sort the list of elements by minus their length and then alphabetically, so you want the key of a string s to be the tuple (-len(s), s). Hence:

>>> l = ['aa','aaa','aaaa','b','bb','z','ccc']
>>> sort_key = lambda s: (-len(s), s)
>>> l.sort(key=sort_key)
>>> l
['aaaa', 'aaa', 'ccc', 'aa', 'bb', 'b', 'z']
Katriel
  • 120,462
  • 19
  • 136
  • 170
6

Design

The keyword here is "stable sorting algorithm". Think of two stable sorting functions:

  • one sorting according to length (maintaining the relative order of entries with equal lengths),
  • the other sorting alphabetically.

In which order would you combine them in order to get the desired order?

Implementation

As others have mentioned, the first sorting function can be called like this:

list.sort(key=len, reverse=True)

The second sorting function can be called like this:

list.sort()

This should be enough to write a complete solution.

Result

If you combine the function in the right way, you should get the following:

>>> l = ['aaa', 'fff', 'bbb', 'ddd', 'e', 'cccc']
# ... sorting functions combined in the right way ...
>>> l
['cccc', 'aaa', 'bbb', 'ddd', 'fff', 'e']
Bolo
  • 11,542
  • 7
  • 41
  • 60
2

In Python, list's sort method has a key which can be used to specify the criteria for sorting. For the problem you describe I would use the key as well as do the normal sorting as follows.

>>> l = ['aa','aaa','aaaa','b','bb','z','ccc']
>>> l.sort(key=len,reverse=True)
>>> l
['aaaa', 'ccc', 'aaa', 'bb', 'aa', 'z', 'b']
>>> l.sort()
>>> l
['aa', 'aaa', 'aaaa', 'b', 'bb', 'ccc', 'z']
>>> 
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • 2
    `l.sort(key=len, reverse=True)` would be better than `l.sort(...); l.reverse()` – Chris Lutz Jan 11 '11 at 09:01
  • After the second sort(), the list is no longer displaying in order of length (longest to shortest). – John Jan 11 '11 at 09:10
  • Call: sortlist(['a','b','bb','ba']) Expected: [bb, ba, b, a] Result: ['a', 'b', 'ba', 'bb'] This is what happen after I tried the code – John Jan 11 '11 at 09:14
  • 1
    @John, the idea here is the use of key=len and reverse=True. What you need to do is call reverse first and then sort based on the key len. – Senthil Kumaran Jan 11 '11 at 09:23
0
a = sorted([["foo", "o"], ["bar2", "yadda"], ["allo","as3r"]], key=len)
b = sorted(a)
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
0

Hints:

mylist = ['one', 'three', 'zero']

mylist.sort(key=len)

print mylist

mylist.reverse()

print mylist

mylist.sort()

print mylist

otherlist = [(2, 'a'), (1, 'a'), (3, 'b'), (3, 'a')]

otherlist.sort()

print otherlist
TryPyPy
  • 6,214
  • 5
  • 35
  • 63