Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
NEW SERVER - 11.9.2020 ... Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
Sorting data the way YOU want it sorted

Pieces of program code ... subroutines, functions, macros, procedures, user commands, methods ... are stored with a name, and within many programming languages are actually stored in the same memory area and under the same structure as other named values - variables. This means that you can pass in a piece of code to another function as a parameter. But why would you want to do that?

Let's take an example.

I've written a generic sorting routine that takes an array (I'm using PHP terms and examples today as I'm running a PHP course this week) as its parameter, and sorts it "in situ". But I want my sort routine to be flexible in how it decided which records come in before (and which come after) each other; the clever bit of sorting is to work out which comparisons to make, and to manage those comparisons to keep them down to a reasonable level. So I'm going to write a seperate function that my generic code can call to make the comparison that I want, and I'm going to pass the name of that function into the generic sorting code.

There's an example I wrote yesterday to illustrate that - source code [here] and run it [here] on our server.

a) The main code calls the sorter routine:

b) The mysorter function collects that name of the comparator as follows:
  function mysorter(&$arr,$codename) {
and then calls the comparator usingthe variable namein which the comparator function is defined:
  $wanttoswap = $codename($arr[$j+1],$arr[$j]);

c) The comparator function actually does the comparison of two items.

The comparator function MUST take the number of parameters, in the order required, that's defined within the sorter function, and it must return a result in the form that that the sorter function expects.

In a practical application, I would NOT write "mysorter". I would use a standard PHP function - but I would still pass in the name of one of my own pieces of code if I wanted a somewhat unusual sort order; my example sorts numbers based on the remainder when the numbers are divided by 10, and that's a good example of the sort of place I would use a mechanism such as this, which is known as a callback. PHP's standard functions which sort using callbacks are usort, uasort and uksort. In each case, you pass them the array to be sorted, and the name of your own function which takes two parameters and returns a -1 / 0 / +1 value to indicate that the first parameter is to be considered to be less than, equal to, or greater than the second one for sorting purposes.

Other examples / articles - in Python, in Lua, and in Perl.
(written 2011-08-05)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H106 - PHP - Arrays
  [4244] Disambiguation - PHP List - (2014-03-07)
  [4072] Splitting the difference with PHP - (2013-04-27)
  [4068] Arrays in PHP - contain different and even mixed data types - (2013-04-24)
  [3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
  [2274] PHP preg functions - examples and comparision - (2009-07-08)
  [2215] If nothing, make it nothing. - (2009-06-02)
  [1614] When an array is not an array - (2008-04-17)
  [1451] More PHP sample and demonstration programs - (2007-12-01)
  [1199] Testing for one of a list of values. - (2007-05-22)
  [1116] PHP adding arrays / summing arrays - (2007-03-23)
  [832] Displaying data at 5 items per line on a web page - (2006-08-14)
  [773] Breaking bread - (2006-06-22)
  [603] PHP - setting sort order with an associative array - (2006-02-13)
  [409] Functions and commands with dangerous names - (2005-08-11)

Back to
New product - ensuring that supply matches demand
Previous and next
Horse's mouth home
Forward to
Melksham Jelly - An Occasional Office for Home Workers
Some other Articles
Are people who walk into Melksham being asked to subsidise parking?
What is the picture?
Report - day trip from Swindon / Chippenham / Melksham to Weymouth
Melksham Jelly - An Occasional Office for Home Workers
Sorting data the way YOU want it sorted
New product - ensuring that supply matches demand
What do I mean when I add things in Perl?
Kennet and Avon - Walk from Bedwyn to Pewsey. TransWilts day out.
How to interact with a Perl program while it is processing data
Speeding up your Perl code
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/3379_Sor ... orted.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat