Re: Sorting a list of names
- To: mathgroup at smc.vnet.net
- Subject: [mg112285] Re: Sorting a list of names
- From: Valeri Astanoff <astanoff at gmail.com>
- Date: Tue, 7 Sep 2010 06:09:18 -0400 (EDT)
- References: <i64ki5$3c1$1@smc.vnet.net>
On 7 sep, 08:02, 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.= Good day, I suggest this way to do it : In[1]:= names = {"Cole Dockter", "Mackenzie W Christensen", "Rebecca Eastham", "David Gartzke", "Matthew Lundberg", "Tyler Hullett", "Cecilia Ford", "Grant A Patterson"}; In[2]:= Sort[names, OrderedQ[{StringSplit[#1][[-1]], StringSplit[#2][[-1]]}] &] Out[2]= {"Mackenzie W Christensen", "Cole Dockter", "Rebecca \ Eastham", "Cecilia Ford", "David Gartzke", "Tyler Hullett", "Matthew \ Lundberg", "Grant A Patterson"} -- V.Astanoff