Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
User defined sorting - what is a callback?

DEFAULT SORTING

Python's list are collections of objects - in other words, they are rather like arrays in other languages taht can hold a whole series of values, and just like arrays you'll want to rearrange them into a know order. Let's say that I have an array called people - well - I can sort it using the sort method.

people = ["John A Smith","Dorothy Esmerelda Davis","Hugo hurst","Peter Piper",
 "Paul Jones","Mary O'Connor"]
people.sort()
print people

And the result is a sorted list of people .... the only problem being that they are sorted by their first names, which isn't usually what you would want.

['Dorothy Esmerelda Davis', 'Hugo hurst', 'John A Smith', "Mary O'Connor", 'Paul Jones', 'Peter Piper']

SORTING WITH A CALLBACK

When you are sorting data, you're really making a whole lot of comparisons between pairs of records and saying "this one comes first", "the other one comes first" or "they are the same as far as my sort is concerned". It's quite easy to write code that tells two records apart in this way, but much more difficult to string together a large number of such comparisons to do it with a larger number of such records, especially if you want to be as efficient as possible about it. So here's the scheme you'll use ..

1. You create your list of data that you need to sort 2. Call Python's sort routine, passing in the name of the method that you've written to tell two members of the list apart 3. Python (internally within sort) makes many calls to your routine, allowing it to put pairs into order

This approach of calling a standard Python routine which in turn calls your routine is known as a callback - it works well and is used elsewhere too. You must, though, be very careful to ensure that your callback routine does behave as it should - in the case of sort, it must take in two objects as parameters, and return a value of -1, 0 or +1 (meaning "this is first", "they are the same in sorting terms" and "this is second").

Here is a Python example of sorting using a callback - it's the same data that I used above, but this time I am sorting by last name.

def bylastname(first, second):
 n1p = first.split(" ")
 # trimtitle(n1p)
 n2p = second.split(" ")
 # trimtitle(n2p)
 if n1p[-1].upper() < n2p[-1].upper(): return -1
 if n1p[-1].upper() > n2p[-1].upper(): return 1
 return 0
people = ["John A Smith","Dorothy Esmerelda Davis","Hugo hurst","Peter Piper",
 "Paul Jones","Mary O'Connor"]
people.sort(bylastname)
print people

Here are the results:

['Dorothy Esmerelda Davis', 'Hugo hurst', 'Paul Jones', "Mary O'Connor", 'Peter Piper', 'John A Smith']

HOW DOES THE CALLBACK REALLY WORK?

The first time you come across a callback, you may find in very hard to get your head around - the idea that Python's sort routine is mixed up in the middle of your code can seem alien. So here - for illustrative purposes - is an example of how I might sort a list WITHOUT using the built in sort routine in Python:

def bylastname(first, second):
 n1p = first.split(" ")
 # trimtitle(n1p)
 n2p = second.split(" ")
 # trimtitle(n2p)
 if n1p[-1].upper() < n2p[-1].upper(): return -1
 if n1p[-1].upper() > n2p[-1].upper(): return 1
 return 0
def mysort(what, how):
 posn = 1
 while posn < len(what):
  p2 = 1
  while p2 < len(what):
   rv = how(what[p2],what[p2-1])
   if rv < 0:
    h = what[p2]
    what[p2] = what[p2-1]
    what[p2-1]=h
   p2 += 1
  posn += 1
people = ["John A Smith","Dorothy Esmerelda Davis","Hugo hurst","Peter Piper",
 "Paul Jones","Mary O'Connor"]
mysort(people,bylastname)
print people

What's the difference? Well - the ONLY differences between that example and the previous one are that I have replaced Python's built in "sort" with my own "mysort" to show you how it works. But beware - mine is far less efficient than the built in one; it will make many times more checks, so please do use the alternative that the clever Python people provide!

SOME NOTES

Just to complete the picture on sorting in Python:

1. You cannort sort a dictionary. But you can get a list of all the keys from a dictionary, sort those, and then trarse the dictionary in the oredr of the sorted keys.

2. You cannort sort a tuple. But you can convert a tuple into a list, sort the list, then create new tuple from the sorted list.

3. Lists are sorted in situ - in other words, in Python the list that's input to the sort routine is also the one in which the output is placed. This was a design choice as the language was written; it differs from other languages such as Perl which generate a new output list containing the sorted data, leaving the original in tact.




See also Learning Python

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Python - More on Collections and Sequences
  [61] - ()
  [386] - ()
  [633] - ()
  [899] - ()
  [1304] - ()
  [1310] - ()
  [1869] - ()
  [1873] - ()
  [2718] - ()
  [2894] - ()
  [2920] - ()
  [2996] - ()
  [3150] - ()
  [3348] - ()
  [3439] - ()
  [3797] - ()
  [4398] - ()
  [4442] - ()

resource index - Python
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

1 unpublished comment pending on this page

edit your own (not yet published) comments

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/python-u ... lback.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard