If, as your question, your interest is only in sorting namedtuples using an alternate key, why not use the sort / sorted key argument with the attrgetter function:
>>> from collections import namedtuple >>> from operator import attrgetter >>> P = namedtuple("P", "xy") >>> p1 = P(1, 2) >>> p2 = P(2, 1) >>> sorted([p1, p2], key=attrgetter("y")) [P(x=2, y=1), P(x=1, y=2)]
You can go even further and define your own sort function:
>>> from functools import partial >>> sortony = partial(sorted, key=attrgetter("y")) >>> sortony([p1, p2]) [P(x=2, y=1), P(x=1, y=2)]
Don O'Donnell
source share