Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #13276 > unrolled thread
| Started by | Francisco Gutierrez <fgutiers2002@yahoo.com> |
|---|---|
| First post | 2013-01-26 06:38 +0000 |
| Last post | 2013-01-28 07:24 +0000 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
sorting? Francisco Gutierrez <fgutiers2002@yahoo.com> - 2013-01-26 06:38 +0000
Re: sorting? awnl <awnl@gmx-topmail.de> - 2013-01-26 21:58 +0000
Re: sorting? Francisco Gutierrez <fgutiers2002@yahoo.com> - 2013-01-28 07:24 +0000
| From | Francisco Gutierrez <fgutiers2002@yahoo.com> |
|---|---|
| Date | 2013-01-26 06:38 +0000 |
| Subject | sorting? |
| Message-ID | <kdvtkk$fp2$1@smc.vnet.net> |
Friends:
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).
Thanks for your help
Francisco
[toc] | [next] | [standalone]
| From | awnl <awnl@gmx-topmail.de> |
|---|---|
| Date | 2013-01-26 21:58 +0000 |
| Message-ID | <ke1jid$jni$1@smc.vnet.net> |
| In reply to | #13276 |
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
[toc] | [prev] | [next] | [standalone]
| From | Francisco Gutierrez <fgutiers2002@yahoo.com> |
|---|---|
| Date | 2013-01-28 07:24 +0000 |
| Message-ID | <ke593f$s84$1@smc.vnet.net> |
| In reply to | #13276 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web