MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Sorting nested lists with strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg116306] Re: Sorting nested lists with strings
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Thu, 10 Feb 2011 05:24:00 -0500 (EST)

list = {{"Paul", 48}, {"Paul", 23}, {"Amanda", 55}, {"Carl", 32}};

Sort defaults to using the first element

Sort[list]

{{"Amanda", 55}, {"Carl", 32}, {"Paul", 23}, {"Paul", 48}}

SortBy[list, First]

{{"Amanda", 55}, {"Carl", 32}, {"Paul", 23}, {"Paul", 48}}

Sort[list, OrderedQ[{#1, #2}] &]

{{"Amanda", 55}, {"Carl", 32}, {"Paul", 23}, {"Paul", 48}}


Bob Hanlon

---- Paperorbifold <kgodelNOSPAM at liberoNOSPAM.it> wrote: 

=============
I've a nested list of this kind:
list={{"Paul",23},{"Amanda",55},{"Carl",32},...}
Suppose I want to perform an alphabetic sort on the first element. Which is 
the condition I've to write in Sort[list,...]?
Actually Sort[list] automatically operates on the second element.
Thanks.
 
-- 
...and I will make them pay for\nwhat they've done! (J.L. Picard)



  • Prev by Date: Re: Sorting nested lists with strings
  • Next by Date: Changing fonts of greek letters
  • Previous by thread: Re: Sorting nested lists with strings
  • Next by thread: Re: Sorting nested lists with strings