Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #15832 > unrolled thread

Re: Sorting nested lists with strings

Started by"Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com>
First post2011-02-10 10:19 +0000
Last post2011-02-11 09:12 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Sorting nested lists with strings "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> - 2011-02-10 10:19 +0000
    Re: Sorting nested lists with strings Paperorbifold <kgodelNOSPAM@liberoNOSPAM.it> - 2011-02-11 09:12 +0000

#15832 — Re: Sorting nested lists with strings

From"Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com>
Date2011-02-10 10:19 +0000
SubjectRe: Sorting nested lists with strings
Message-ID<ij0e47$998$1@smc.vnet.net>
I'm not sure why you think Sort sorts on the second field of the list.
It doesn't:

In[534]:= list = {{"Paul", 23}, {"Amanda", 5}, {"Carl", 32}};

In[535]:= Sort[list]

Out[535]= {{"Amanda", 5}, {"Carl", 32}, {"Paul", 23}}



If you'd like to sort on another field (say the second) you could
change the ordering function of Sort or change a similar function in
Sortby (check the documentation; Sort and SortBy use slightly
different principles):

In[536]:= Sort[list, OrderedQ[{#1[[2]], #2[[2]]}] &]

Out[536]= {{"Amanda", 5}, {"Paul", 23}, {"Carl", 32}}

In[537]:= SortBy[list, #[[2]] &]

Out[537]= {{"Amanda", 5}, {"Paul", 23}, {"Carl", 32}}

Cheers -- Sjoerd

On Feb 9, 8:09 am, Paperorbifold <kgodelNOS...@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)

[toc] | [next] | [standalone]


#15852

FromPaperorbifold <kgodelNOSPAM@liberoNOSPAM.it>
Date2011-02-11 09:12 +0000
Message-ID<ij2uhh$7fd$1@smc.vnet.net>
In reply to#15832
Sjoerd C. de Vries wrote:

> I'm not sure why you think Sort sorts on the second field of the list.
> It doesn't:
> 
> In[534]:= list = {{"Paul", 23}, {"Amanda", 5}, {"Carl", 32}};
> 
> In[535]:= Sort[list]
> 
> Out[535]= {{"Amanda", 5}, {"Carl", 32}, {"Paul", 23}}

Of course you're right! Maybe I was really tired when I sent the message.
It's clear that the option that works immediately is Sort@list (and the 
equivalent Sort[list]).
Thanks to everybody for all the answers.

-- 
...and I will make them pay for what they've done! (J.L. Picard)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web