Windows の Explorer のようなソートを行うための比較関数

def intelligentcompare(s1, s2, ignorecase = False):
	"""\
	This function automatically detects numerical string for intelligent comparison.
	>>> cmp('2. a title of 2nd chapter', '10. a title of 10th chapter')
	1
	>>> intelligentcompare('2. a title of 2nd chapter', '10. a title of 10th chapter')
	-1
	>>> cmp('2.0', '2')
	1
	>>> intelligentcompare('2.0', '2')
	0
	>>> cmp(2.0, 2)
	0
	>>> intelligentcompare(2.0, 2)
	0
	"""
	if type(s1) not in types.StringTypes or type(s2) not in types.StringTypes:
		return cmp(s1, s2)
	compare = ignorecase and (lambda i, j: cmp(i.lower(), j.lower())) or cmp
	t1 = filter(None, pattern_of_numeric.split(s1))
	t2 = filter(None, pattern_of_numeric.split(s2))
	if len(t1) != len(t2):
		return compare(s1, s2)
	for i in xrange(len(t1)):
		c = 0
		try:
			c = cmp(float(t1[i]), float(t2[i]))
		except ValueError:
			c = compare(t1[i], t2[i])
		if c != 0:
			return c
	return 0