Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Sorting people by their names

What are their names?Please sort into order:

Tom Pearce
Bill Brewer
Peter Gurney
Peter Davy
Daniel Whiddon
Harry Hawk
Uncle Tom Cobbley

What would you do?

Bill Brewer
Uncle Tom Cobbley
Peter Davy
Peter Gurney
Harry Hawk
Tom Pearce
Daniel Whiddon

That's not a standard sort order for strings of text ... and if you've got each complete name in a string (as opposed to separate forename and surname fields) you would need to sort it by the last space separated string on each line.

Sort routines in most of the language we teach default to sorting strings Asciibetically (i.e alphabetically, but with lower case after upper case), and allow callback routines to be used to alter this behavior. What that means is that you call the sort routine, but pass in as a parameter a piece of code which tells the sort routine how it should grade pairs of records - with the sort routine then working out which of the pairs it needs to compare and doing all the clever control and management of the sorting process.

There's a new example of this (sorting by surnames) [here] from last week's Python course. You'll note that - in Python - I've called the cmp function internally to order two records, as once I have extracted the surname, I'm back to a conventional sort in this case. cmp returns -1, 0 or +1 to indicate that the first parameter should come first, the two are equal in sort terms, or the first parameter should come second.


Some of our local characters at Widdicome (or should that be Widecombe?) Fair vary the spelling of their surnames - I've see Tom Cobleigh as well as Tom Cobbley, for example - and other issues arise to when sorting names ...

Where several members of the same family / with the same surname are involved, you'll want to sort by the name as a whole once you've grouped by surname - that can often be a default sort:

def bysurname(this, that) :
  sn1 = (author[this].split())[-1]
  sn2 = (author[that].split())[-1]
  vx = cmp(sn1,sn2)
  if vx:
    return vx
  return cmp(this,that)


And you have matters such as differing cases, and people who have entered just initials rather that their forenames or who have (as is case of our Tom above) abbreviated Thomas to Tom and added a Titular Mr, Mrs, Ms, or Uncle.

But a Royal visit to Widdicome Fair bringing some overseas dignitaries would give some interesting people to sort. Just imagine ...

Elizabeth the Second
HRH Prince Philip
Lord Kinnock of Bedwellty
George Bush Senior
Archbishop of Canterbury
Benedict XVI
Mel Stride, MP
(written 2010-07-29, updated 2010-07-30)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q110 - Object Orientation and General technical topics - Programming Algorithms
  [3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
  [3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
  [3451] Why would you want to use a Perl hash? - (2011-09-20)
  [3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
  [3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
  [2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
  [2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
  [2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
  [2586] And and Or illustrated by locks - (2010-01-17)
  [2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
  [2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
  [2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [1840] Validating Credit Card Numbers - (2008-10-14)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
  [1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
  [642] How similar are two words - (2006-03-11)
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [202] Searching for numbers - (2005-02-04)

Y111 - Python - More on Collections and Sequences
  [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)
  [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)


Back to
Exclamation marks and question marks on ruby method names
Previous and next
or
Horse's mouth home
Forward to
Global Computer Maintenance Department
Some other Articles
Programming Standards from the start!
The Land of the Black Labrador
LAMP - Linux, Apache, MySQL, PHP - install, configure, administer
Global Computer Maintenance Department
Sorting people by their names
Exclamation marks and question marks on ruby method names
Alternative loops and conditionals in Ruby and Perl
Wiltshire Council ask how they can help businesses
Dates and times in Python
Should Python classes each be in their own file?
4291 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 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2894_Sor ... names.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat