MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Re: easiest way to sort a list?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg18414] Re: [mg18343] Re: [mg18324] Re: [mg18308] easiest way to sort a list?
  • From: Andrew Watson <abwatson at mail.arc.nasa.gov>
  • Date: Wed, 7 Jul 1999 00:11:10 -0400
  • Sender: owner-wri-mathgroup at wolfram.com

At 11:13 AM -0700 6/30/99, Tomas Garza wrote:
>Andrew,
>I'm afraid your proposed solution works in the particular example you used,
>but it doesn't in general. Cf. the following:
>
>In[1]:=
>in = {2, 1}
>Out[1]=
>{2, 1}
>In[2]:=
>elements = Union[in]
>Out[2]=
>{1, 2}
>In[3]:=
>positions = Flatten[First[Position[in, #]] & /@ elements]
>Out[3]=
>{2, 1}
>In[4]:=
>in[[positions]]
>Out[4]=
>{1, 2}
>
>The correct solution is {2,1}.
>
>Tomas Garza
>Mexico City

You are quite right. I left out the Sort. This should do it (I think this
is essentially the same as another submission).

OrderedUnion[list_] :=
  list[[Sort[Flatten[First[Position[list, #]] & /@ Union[list]]]]]

-Andrew

Andrew B. Watson
MS 262-2
NASA Ames Research Center
Moffett Field, CA 94035-1000
(650) 604-5419	(650) 604-0255 fax
abwatson at mail.arc.nasa.gov	http://vision.arc.nasa.gov/


  • Prev by Date: Re(2): Re: easiest way to sort a list?
  • Next by Date: Contour Integrals
  • Previous by thread: Re: Re: easiest way to sort a list?
  • Next by thread: Re: Re: easiest way to sort a list?