Re: Sorting question
- To: mathgroup at smc.vnet.net
- Subject: [mg52786] Re: Sorting question
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Mon, 13 Dec 2004 04:22:41 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 12/11/04 at 5:23 AM, jdedguard-agent at yahoo.co.uk (Nimrod) wrote: >A simple sorting question that has me stumped as to how to do it both >quickly and elegantly. >I have a list of data pairs of the form (distance, time). I want >to sort on the distance element and then remove all pairs where the >time element is out of order with the distance element. For >example ((1,1),(2,3),(3,2),(4,4)) --> ((1,1),(4,4)). The actual >lists are much longer. I would code this as Select[data[[Ordering[data[[All,1]]]]], OrderedQ@#&] where data is the list of pairs to be sorted. Note, this coding matches your text, i.e., all pairs where distance (the first element) is ordered with the second element (time) are selected. That is it will retain {2,3} since OrderedQ@{2,3} returns true. Your example shows pairs with both elements being equal as the only ones retained. This could be coded as: Select[data[[Ordering[data[[All,1]]]]], Equal@@#&] -- To reply via email subtract one hundred and four