RadiantHex
RadiantHex

Reputation: 25597

Problem sorting list of strings - Python

I have a list of strings:

cards = ['2S', '8D', '8C', '4C', 'TS', '9S', '9D', '9C', 'AC', '3D']

and the order in which I want to display the cards:

CARD_ORDER = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A']

This is how I'm trying to order the list:

sorted(cards, lambda x,y: CARD_ORDER.index(x[0]) >= CARD_ORDER.index(y[0]) )

Unfortunately this does not seem to work....

or more precisely the list stays exactly the same, sorted(cards) works fine instead.

Any ideas?

Upvotes: 0

Views: 304

Answers (2)

SilentGhost
SilentGhost

Reputation: 320019

it's

sorted(cards, key=lambda x: CARD_ORDER.index(x[0]))

key parameter accepts a single value, by which to sort the main iterable. You're probably trying to use cmp parameter which is not recommended for quite some time.

Upvotes: 8

Howard
Howard

Reputation: 39217

Try

sorted(cards, key = lambda x: CARD_ORDER.index(x[0]) )

Upvotes: 2

Related Questions