Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
User defined sorting - what is a callback?


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"]
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']


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"]
print people

Here are the results:

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


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]
   p2 += 1
  posn += 1
people = ["John A Smith","Dorothy Esmerelda Davis","Hugo hurst","Peter Piper",
 "Paul Jones","Mary O'Connor"]
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!


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
  [4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
  [4398] Accessing variables across subroutine boundaries - Perl, Python, Java and Tcl - (2015-01-18)
  [3797] zip in Python - (2012-07-05)
  [3439] Python for loops - applying a temporary second name to the same object - (2011-09-14)
  [3348] List slices in Python - 2 and 3 values forms, with an uplifting example - (2011-07-06)
  [3150] Python dictionaries - mutable and immutable keys and values - (2011-01-29)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2894] Sorting people by their names - (2010-07-29)
  [2718] Python - access to variables in the outer scope - (2010-04-12)
  [1873] List Comprehensions in Python - (2008-11-06)
  [1869] Anonymous functions (lambdas) and map in Python - (2008-11-04)
  [1310] Callbacks - a more complex code sandwich - (2007-08-19)
  [1304] Last elements in a Perl or Python list - (2007-08-16)
  [899] Python - extend v append on a list - (2006-10-20)
  [633] Copying a reference, or cloning - (2006-03-05)
  [386] What is a callback? - (2005-07-22)
  [61] Python is a fabulous language - (2004-09-24)

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., 2019: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 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