Sorting people by their names
Archive - Originally posted on "The Horse's Mouth" - 2010-07-29 07:39:04 - Graham Ellis
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