How can I Implement a SortedContainers.SortedListWithKey with multiple sort orders?

276 Views Asked by At

The code below would fail due to a "TypeError: bad operand type for unary -: 'str'" exception, but it illustrates what I am trying to achieve.

from sortedcontainers import SortedListWithKey
contacts = SortedListWithKey(key=lambda val:(val.FirstName, -val.LastName))
class Person(object):
    pass

p1 = Person()
p1.FirstName = 'Amy'
p1.LastName = 'Obama'

p2 = Person()
p2.FirstName = 'Bob'
p2.LastName = 'Obama'

p3 = Person()
p3.FirstName = 'Bob'
p3.LastName = 'Trump'

p4 = Person()
p4.FirstName = 'Amy'
p4.LastName = 'Trump'

contacts.add(p1)
contacts.add(p2)
contacts.add(p3)
contacts.add(p4)

for p in contacts:
    print(p.FirstName, p.LastName)

The intended outputs are:

Amy Trump
Amy Obama
Bob Trump
Bob Obama

The code above would work if Person.LastName is a numeric type member.

If I were not implementing a SortedList but just a built-in un-sorted list, I can simply do

contacts.sort(key = operator.itemgetter(0))
contacts.sort(key = operator.itemgetter(1), reverse=True)

But this option is not possible for my situation.

SortedContainers website: http://www.grantjenks.com/docs/sortedcontainers/index.html

1

There are 1 best solutions below

0
On

One not elegant and not performant way to do it is

contacts = SortedListWithKey(key=lambda val:(val.FirstName, [-ord(x) for x in val.LastName]))