[Tutor] user defined compare for sorting

Rick Pasotto rick@niof.net
Mon, 9 Apr 2001 16:34:52 -0400


I have a list of dictionaries. How can I specify at runtime the key to
use for sorting the list?

lst[ [ 'A':'a', 'B':'2', 'C':'z' ]
     [ 'A':'b', 'B':'3', 'C':'x' ]
	 [ 'A':'c', 'B':'1', 'C':'y' ] ]

How can I pass 'A', 'B', or 'C' to 'key' in the compare function?

def mycmp(a,b): cmp(a[key],b[key])

-- 
"Capitalism is a social system based on the recognition of individual
rights, including property rights, in which all property is privately
owned."
		-- Ayn Rand
		   Rick Pasotto email: rickp@telocity.com