Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: sorting problem
Replies: 1   Last Post: Apr 28, 2013 5:17 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View  
Simons, F.H.

Posts: 107
Registered: 12/7/04
Re: sorting problem
Posted: Apr 28, 2013 5:17 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Here is one solution, assuming that all elements in your list distances
are non-negative:

In[1]:= distances={0,Sqrt[2],Sqrt[2],0,0,Sqrt[2],2,Sqrt[2]};

In[2]:= Table[Position[distances,x,1],{x, Union[distances]}] // Flatten

Out[2]= {1,4,5,7,2,3,6,8}

Regards,

Fred Simons
Eindhoven University of Technology

Op 28-4-2013 6:57, S schreef:
> Hello
>
> I need to sort a list in increasing magnitude. If two elements in the list are the same, the element which appears EARLIER in the list should appear first. Finally, I need the indices of the sorted list according to the above two rules.
>
> e.g. for input
> distances = {0, Sqrt[2], Sqrt[2], 0, 0, Sqrt[2], 2, Sqrt[2]}
> I need the answer to be
> k={1,4,5,2,3,6,8,7}
>
> I tried the following commands:
>
> distances = {0, Sqrt[2], Sqrt[2], 0, 0, Sqrt[2], 2, Sqrt[2]}
> Sorteddistances = Sort[distances, Less]
> k = Ordering[distances]
>
> I get output k={1, 4, 5, 7, 2, 3, 6, 8}.
>
> However, I want the output k={1,4,5,2,3,6,8,7}
>
> Can someone please suggest how to do this.
>
> Thanks
>
> S
>





Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.