Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: sorting?
Replies: 3   Last Post: Jan 28, 2013 2:24 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Francisco Gutierrez

Posts: 100
Registered: 5/31/05
Re: sorting?
Posted: Jan 28, 2013 2:24 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Works perfect Albert, thanks!
Francisco

From: awnl <awnl@gmx-topmail.de>
Sent: Saturday, January 26, 2013 4:59 PM
Subject: Re: sorting?

Hi,

> I have a large list of simple numerical lists, and I want to sort it
> using two criteria. in the simplest case, suppose the sublists are of
> length two, and that my first criterion is the quotient of part one
> by part two, and my second criterion is the size of part two (no
> zeros in part 2. I want to sort using the first criterion, and then
> break the ties using the second one. So for example if I had
> lalista={{0,5},{8,8},{2,2},{3,5},{1,3},{3,9}}, the sorting should
> return


> {{8,8},{2,2},{3,5},{3,9},{1,3},{0,5}}

> Of course this can be done, but I wonder if it is a way to make this
> only with native Mathematica tools (wich is always far more
> efficient).



I think you are looking for SortBy, which can be given a list of pure
functions to define the sorting, e.g. like this (the minus signs reverse
the sorting so I get what you want, it might need adoption depending on
what you really want...):

SortBy[lalista, {-#[[1]]/#[[2]] &, -#[[2]] &}]

hth,

albert




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.