Re: Sorting a list of names
- To: mathgroup at smc.vnet.net
- Subject: [mg112284] Re: Sorting a list of names
- From: Simon <simonjtyler at gmail.com>
- Date: Tue, 7 Sep 2010 06:09:07 -0400 (EDT)
- References: <i64ki5$3c1$1@smc.vnet.net>
On Sep 7, 4:02 pm, Jon Joseph <josco.... at gmail.com> wrote:
> All: I have a list of names read in as strings with first names first.
> Can anyone suggest a mechanism to sort the list by the last names with
> the final output being the entire name?
>
> Starting array:
>
> names = {"Cole Dockter", "Mackenzie Christensen", "Rebecca Eastham",
> "David Gartzke", \
> "Matthew Lundberg", "Tyler Hullett", "Cecilia Ford", "Grant Patterson"}
>
> Ending array:
>
> names_sorted = {"Mackenzie Christensen", "Cole Dockter", "Rebecca
> Eastham", "Cecilia Ford", "David Gartzke", "Tyler Hullett", "Matthew
> Lundberg", "Grant Patterson"}
>
> As a note Sort[StringSplit[names][[All, 2]]] does exactly what I want
> except it drops the first name on output.=
Try
SortBy[names, Reverse[StringSplit[#]] &]
It will sort by Last name then first name.
If you have names with more than two parts you might want to be a
little more sensible about the way you store the data.
It's normally best to sort the Surname and the Given names separately.