Welcome, guest | Sign In | My Account | Store | Cart

How do you sort efficiently a list of (x, y) coordinates? Now generalize that to a list of (dictionaries, or lists, or classes). I HATE not being able to sort via any attribute, so I wrote a class-function to do it for me.

Python, 66 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python

# This is a class designed to sort things via an index or an attribute.
# Thus, you can sort lists of classes or lists of dicts or lists of tuples
# without much trouble.
class Sorter :

	# Initialize self.__attribute .  It will be abused later.
	def __init__ (self) :
		self.__attribute = None

	# Notice how __compare is dependant upon self.__attribute
	def __compare (self, x, y) :
		return cmp(x[self.__attribute], y[self.__attribute])

	# Pass the sort function the __compare function defined above.
	def __call__ (self, data, attribute = None) :
		if attribute == None :
			data.sort()

		else :
			self.__attribute = attribute
			data.sort(self.__compare)

			# This is useful if you want to inline things.
			# I don't know how much innefficiency it introduces.
			# Is this just a new reference?  (that would be cool.)
			return data
			

# Instanciate the Sorter class once in the module.
# All calls will subsequently abuse this instance.
sort = Sorter()


if __name__ == '__main__' :

	list = [(1, 2), (4, 8), (0, 3)]
	dict = [{'a': 3, 'b': 4}, {'a': 5, 'b': 2}, {'a': 0, 'b': 0}, {'a': 9, 'b': 9}]
	dumb = [1, 4, 6, 7, 2, 5, 9, 2, 4, 6]

	print

	print 'list normal:', list
	sort(list, 0)
	print 'sort by [0]:', list
	sort(list, 1)
	print 'sort by [1]:', list

	print
	print

	print "dict normal:", dict
	sort(dict, 'a')
	print "sort by 'a':",  dict
	sort(dict, 'b')
	print "sort by 'b':",  dict

	print
	print

	print 'dumb normal:', dumb
	sort(dumb)
	print 'normal sort:', dumb

	print

I don't know how much innefficiency the return of data introduces. It might only return a reference. That would be cool. But if it recreates the entire thing, then I should remove it.

But hey, I use this all the time, and I love it.

Created by Matthew Wood on Thu, 23 Aug 2001 (PSF)
Python recipes (4591)
Matthew Wood's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks