[Mindwrapper] sorting listnodes
Status: Alpha
Brought to you by:
donnal
|
From: Donnal W. <don...@ya...> - 2003-04-17 20:38:38
|
I probably won't check in the revised branch module today, because
I want to change the observable mechanism for listnode first, but
today I added two new methods to listnode:
def Sorted(self, apath=''):
temp = [(_getapathval(o, apath), o) for o in self]
temp.sort()
return [t[1] for t in temp]
def Sort(self, apath=''):
self[:] = self.Sorted(apath)
The Sorted() method returns a list that is sorted on any arbitrary
attribute path in each item, while Sort() sorts the listnode in
place using the same criteria. Usage would be like this:
x.Sort('id.name.last')
or
y = x.Sorted('id.name.last')
The _getapathval() function returns the value of the node specified
by the attribute path (apath) string. It is defined as:
def _getapathval(obj, apath):
if apath:
for attr in apath.split('.'):
obj = getattr(obj, attr)
return obj.Get()
The sort methods use the "decorate-sort-undecorate" (DSU) idiom
found in PythonIAN pp 400-401. This provides an efficient way of
sorting on any arbitrary components of the list items.
Regards,
=====
Donnal Walter
Arkansas Children's Hospital
|