Re: Sort List of Lists

• To: mathgroup at smc.vnet.net
• Subject: [mg24820] Re: [mg24809] Sort List of Lists
• From: "Richard Finley" <rfinley at medicine.umsmed.edu>
• Date: Sun, 13 Aug 2000 23:49:44 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Dana,

If the 2nd elements are numeric, you can use:

Sort[list, (#2[[2]] > #1[[2]]) &]

but be careful....this won't work if the second elements are not numeric,
eg. list = {{4,c},{2,d},{7,a}}

In that case you could use the following which is more general and will work in either case:

Sort[list, (Order[#1[[2]], #2[[2]]] == 1) &]

Basically, the second argument to the Sort function is an order function that defines the order of elements by a Boolean function.

regards,  RF

>>> "dana2" <dan_news at hotmail.com> 08/13/00 01:16AM >>>
Hello.  Could someone please give me a clue on how to sort a list based on
the Second Element in a List:
I think one needs to use a "pure function" but I am too new with Mathematica (v4)
(Trying to learn though) :>)

For Example, I would like to sort this list based on the second element in
each sub-list in "Descending" order:

{{9, 2}, {1, 6}, {2, 4}}

Results would be:
{ {1,6},{2,4},{9,2} }

I think I will learn a lot about Lists, Sorting, and Pure Functions all in
one shot  if I could get a hint on how to do this.
Thank you  very much.  Dana

```

• Prev by Date: RE: Sort List of Lists
• Next by Date: RE: Sort List of Lists
• Previous by thread: RE: Sort List of Lists
• Next by thread: RE: Sort List of Lists