[Tutor] I'd like to sort (symbol, address) set by address [using
cmp()]
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Mon, 15 Jul 2002 12:09:43 -0700 (PDT)
On Mon, 15 Jul 2002, Sean 'Shaleh' Perry wrote:
> > What I'd like to do is to sort my symbol files by address(second
> > item). I think first I have to convert second item value to integer.
> > so Is there any methond that change input(hex) to integer ?
> >
> > And second, how can I sort by second item ?
> >
>
> rather than shifting your data, just adjust the sort function.
>
> >>> items = [('sean', 1), ('lesley', 5), ('bob', 2), ('jane', 4)]
> >>> def sorter(a, b):
> ... if a[1] < b[1]:
> ... return -1
> ... elif a[1] == b[1]:
> ... return 0
> ... else:
> ... return 1
> ...
By the way, we can also write this sort function by taking advantage of
Python's cmp() comparison function:
###
def compare_by_second(a, b):
return cmp(a[1], b[1])
###
which, if I didn't put any typos in there, should do the same thing as
Sean's sorter() function.
Hope this helps!