Need customised sort

bvdpoel at uniserve.com bvdpoel at uniserve.com
Sat Apr 13 17:05:16 EDT 2002


Terry Reedy wrote:
> 
> <bvdpoel at uniserve.com> wrote in message
> news:3CB884C7.EE7E04D at uniserve.com...
> > I have a list like this:
> >
> > [ [0, x, [0, y, [4, n] ...]
> 
> I am assuming that you mean like this:
> 
> [ [0,x], [0,y], [4,n], ...]
> 
> If so, it is *possible* that the built in [].sort(cmpfunc) will work
> for you *if* you supply the optional custom compare function cmpfunc()
> such as:
> 
> def mycompare(a,b): return cmp(a[0],b[0])
> 
> However, read the reference manual for the Python version you are
> using and test.  I believe (but may be wrong) that .sort() is now
> order preserving but was not always.  If not, you will have to find
> one or devise a hack to make the sort keys unique.  I believe that one
> of a[0]<b[0] or a[0]<=b[0] may work, but again check specific
> reference.
> 
> Terry J. Reedy

Thanks all. After posting a question it's always appropriate to think
about it some more:) Of course, my problem was that old thing DATA
STRUCTURES. I changed to a dict of lists using the event offset as the
dict key. That way multiple events at one offset are all stored togther
and I can parse them later in the proper order.

Sorry for wasting your time with my dumb idea! NOTE TO SELF: Think more
before posting.

-- 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at uniserve.com
WWW:   http://users.uniserve.com/~bvdpoel



More information about the Python-list mailing list