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


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

SortBy

Started bygraser <graser@gmail.com>
First post2011-04-15 07:56 +0000
Last post2011-04-16 11:32 +0000
Articles 6 — 6 participants

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


Contents

  SortBy graser <graser@gmail.com> - 2011-04-15 07:56 +0000
    Re: SortBy Peter Pein <petsie@dordos.net> - 2011-04-16 11:34 +0000
    Re: SortBy Albert Retey <awnl@gmx-topmail.de> - 2011-04-16 11:38 +0000
    Re: SortBy Peter Breitfeld <phbrf@t-online.de> - 2011-04-16 11:38 +0000
    Re: SortBy "Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com> - 2011-04-16 11:39 +0000
    Re: SortBy Helen Read <readhpr@gmail.com> - 2011-04-16 11:32 +0000

#1689 — SortBy

Fromgraser <graser@gmail.com>
Date2011-04-15 07:56 +0000
SubjectSortBy
Message-ID<io8to0$qbf$1@smc.vnet.net>
Dear Mathematica group,

I have a simple question for you.

Let's say there is a list like

KS = {{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32, 1}};

I want to sort it by second element.

I can use

Sort[KS, #2[[2]] > #1[[2]] &]

It gives out like

{{500, 23, 5}, {40, 32, 1}, {300, 48, 2}, {120, 55, 7}}

But if I want to use SortBy, how to do that?

SortBy[KS, ??]

Thanks!

[toc] | [next] | [standalone]


#1713

FromPeter Pein <petsie@dordos.net>
Date2011-04-16 11:34 +0000
Message-ID<iobusj$bbu$1@smc.vnet.net>
In reply to#1689
Am 15.04.2011 09:56, schrieb graser:
> Dear Mathematica group,
>
> I have a simple question for you.
>
> Let's say there is a list like
>
> KS = {{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32, 1}};
>
> I want to sort it by second element.
>
> I can use
>
> Sort[KS, #2[[2]]>  #1[[2]]&]
>
> It gives out like
>
> {{500, 23, 5}, {40, 32, 1}, {300, 48, 2}, {120, 55, 7}}
>
> But if I want to use SortBy, how to do that?
>
> SortBy[KS, ??]
>
> Thanks!
>
>

according to http://reference.wolfram.com/mathematica/ref/SortBy.html 
the following should give True as result:

In[70]:= Sort[KS, #2[[2]] > #1[[2]] &] === KS[[Ordering[KS[[All, 2]]]]] 
=== SortBy[KS, #[[2]] &] === SortBy[KS, Rest]
Out[70]= True

hth,
Peter

[toc] | [prev] | [next] | [standalone]


#1725

FromAlbert Retey <awnl@gmx-topmail.de>
Date2011-04-16 11:38 +0000
Message-ID<iobv34$bgp$1@smc.vnet.net>
In reply to#1689
Am 15.04.2011 09:56, schrieb graser:
> Dear Mathematica group,
>
> I have a simple question for you.
>
> Let's say there is a list like
>
> KS = {{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32, 1}};
>
> I want to sort it by second element.
>
> I can use
>
> Sort[KS, #2[[2]]>  #1[[2]]&]
>
> It gives out like
>
> {{500, 23, 5}, {40, 32, 1}, {300, 48, 2}, {120, 55, 7}}
>
> But if I want to use SortBy, how to do that?
>

While there are functions First and Last, there is no Second and Third, 
but maybe that's an inexpensive possibility to increase the number of 
functions in the next version even more :-).

But you can use a pure function:

SortBy[KS, #[[2]] &]

hth,

albert

[toc] | [prev] | [next] | [standalone]


#1727

FromPeter Breitfeld <phbrf@t-online.de>
Date2011-04-16 11:38 +0000
Message-ID<iobv3s$bh1$1@smc.vnet.net>
In reply to#1689
graser wrote:

> Dear Mathematica group,
>
> I have a simple question for you.
>
> Let's say there is a list like
>
> KS = {{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32, 1}};
>
> I want to sort it by second element.
>
> I can use
>
> Sort[KS, #2[[2]] > #1[[2]] &]
>
> It gives out like
>
> {{500, 23, 5}, {40, 32, 1}, {300, 48, 2}, {120, 55, 7}}
>
> But if I want to use SortBy, how to do that?
>
> SortBy[KS, ??]
>
> Thanks!
>

SortBy[KS, #[[2]]&]

-- 
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de

[toc] | [prev] | [next] | [standalone]


#1728

From"Sjoerd C. de Vries" <sjoerd.c.devries@gmail.com>
Date2011-04-16 11:39 +0000
Message-ID<iobv4n$bh3$1@smc.vnet.net>
In reply to#1689
Easy indeed:

SortBy[{{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32,
   1}}, #[[2]] &]

Cheers -- Sjoerd

On Apr 15, 9:56 am, graser <gra...@gmail.com> wrote:
> Dear Mathematica group,
>
> I have a simple question for you.
>
> Let's say there is a list like
>
> KS = {{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32, 1}};
>
> I want to sort it by second element.
>
> I can use
>
> Sort[KS, #2[[2]] > #1[[2]] &]
>
> It gives out like
>
> {{500, 23, 5}, {40, 32, 1}, {300, 48, 2}, {120, 55, 7}}
>
> But if I want to use SortBy, how to do that?
>
> SortBy[KS, ??]
>
> Thanks!

[toc] | [prev] | [next] | [standalone]


#1736

FromHelen Read <readhpr@gmail.com>
Date2011-04-16 11:32 +0000
Message-ID<iobuo9$b8e$1@smc.vnet.net>
In reply to#1689
On 4/15/2011 3:56 AM, graser wrote:
> Dear Mathematica group,
>
> I have a simple question for you.
>
> Let's say there is a list like
>
> KS = {{300, 48, 2}, {500, 23, 5}, {120, 55, 7}, {40, 32, 1}};
>
> I want to sort it by second element.
>
> I can use
>
> Sort[KS, #2[[2]]>  #1[[2]]&]
>
> It gives out like
>
> {{500, 23, 5}, {40, 32, 1}, {300, 48, 2}, {120, 55, 7}}
>
> But if I want to use SortBy, how to do that?
>
> SortBy[KS, ??]
>
> Thanks!
>
>

SortBy[KS, #[[2]] &]

-- 
Helen Read
University of Vermont

[toc] | [prev] | [standalone]


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


csiph-web