Re: SortBy for multiple key sorts
- To: mathgroup at smc.vnet.net
- Subject: [mg124042] Re: SortBy for multiple key sorts
- From: Bob Hanlon <hanlonr357 at gmail.com>
- Date: Sat, 7 Jan 2012 05:17:02 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201201060916.EAA26845@smc.vnet.net>
pts = {{0, 2}, {1, 2}, {2, 2}, {4, 1}, {3, 1}, {6, 1}, {5, 1}, {7, 0}, {-1,
0}};
sortByYthenX = SortBy[pts, {Min[#[[2]]] &, Min[#[[1]]] &}]
{{-1, 0}, {7, 0}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {0, 2}, {1, 2}, {2, 2}}
The Min isn't doing anything useful
sortByYthenX ==
SortBy[pts, {#[[2]], #[[1]]} &] ==
SortBy[pts, {Last, First}] ==
Reverse /@ Sort[Reverse /@ pts]
True
Bob Hanlon
On Fri, Jan 6, 2012 at 4:16 AM, Chris Young <cy56 at comcast.net> wrote:
> Finally figured out a reasonably simple way to sort by two keys, in
> this case by the y-coordinates, and then within that sorting, by
> x-coordinates. This is just a standard way to sort points: from bottom
> to top in rows, and within each row from left to right. Why on earth
> can't Sort take a list of ordering functions so that the same thing can
> be done there?
>
> In[1200]:= pts =
> {
> {0, 2}, {1, 2}, {2, 2},
> {4, 1}, {3, 1}, {6, 1}, {5, 1},
> {7, 0}, {-1, 0}
> }
>
> In[1206]:= sortByYthenX = SortBy[pts, {Min[#[[2]]] &, Min[#[[1]]] &}]
>
> Out[1206]= {{-1, 0}, {7, 0}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {0,
> 2}, {1, 2}, {2, 2}}
>
- References:
- SortBy for multiple key sorts
- From: Chris Young <cy56@comcast.net>
- SortBy for multiple key sorts