Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7251

Re: reliably sort?

From Andy Ross <andyr@wolfram.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: reliably sort?
Date 2011-12-16 10:53 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jcf804$6q4$1@smc.vnet.net> (permalink)
References <201112141102.GAA10230@smc.vnet.net> <201112150953.EAA22876@smc.vnet.net>

Show all headers | View raw


Personally I prefer DeleteDuplicates for this. At least for packed 
arrays it tends to be about twice as fast.  For arbitrary arrays it can 
be many times faster (though I'm sure instances could be found where it 
loses).

In[28]:= data = RandomChoice[{"A", "B", "C"}, 10^7];

In[29]:= DeleteDuplicates[data] // AbsoluteTiming

Out[29]= {0.609038, {"B", "A", "C"}}

In[30]:= Tally[data][[All, 1]] // AbsoluteTiming

Out[30]= {2.946952, {"B", "A", "C"}}

-Andy

On 12/15/2011 3:53 AM, DrMajorBob wrote:
> Union always sorts in "canonical order".
>
> If you want unsorted order:
>
> Tally[Join[max,mins]][[All,1]]
>
> Bobby
>
> On Wed, 14 Dec 2011 05:02:50 -0600, Michael Stern<nycstern@gmail.com>
> wrote:
>
>> I have noticed that if combine two lists of date/data objects using
>> Union[], the result is sorted by date.
>>
>> For example
>>
>> In[]:= mins={{{2001, 1, 31}, 0.993268}, {{2002, 3, 31}, 1.01395},
>> {{2003, 6, 30},
>>     1.08647}, {{2005, 11, 30}, 1.14752}, {{2006, 9, 30},
>>     1.18938}, {{2007, 2, 28}, 1.19658}, {{2008, 1, 31},
>>     1.20432}, {{2011, 1, 31}, 1.37501}};
>> In[]:= maxs={{{2000, 10, 31}, 1.01816}, {{2001, 12, 31}, 1.02714},
>> {{2004, 2, 29},
>>      1.12702}, {{2005, 3, 31}, 1.16986}, {{2010, 10, 31}, 1.39026}};
>>
>> In[]:= Union[maxs, mins]
>>
>> Out[]= {{{2000, 10, 31}, 1.01816}, {{2001, 1, 31},
>>     0.993268}, {{2001, 12, 31}, 1.02714}, {{2002, 3, 31},
>>     1.01395}, {{2003, 6, 30}, 1.08647}, {{2004, 2, 29},
>>     1.12702}, {{2005, 3, 31}, 1.16986}, {{2005, 11, 30},
>>     1.14752}, {{2006, 9, 30}, 1.18938}, {{2007, 2, 28},
>>     1.19658}, {{2008, 1, 31}, 1.20432}, {{2010, 10, 31},
>>     1.39026}, {{2011, 1, 31}, 1.37501}}
>>
>> Is this reliable behavior? Is there any case in which Union would /not/
>> return results that have been properly sorted by date?
>>
>> Thank you,
>>
>> -Michael Stern
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: reliably sort? Andy Ross <andyr@wolfram.com> - 2011-12-16 10:53 +0000

csiph-web