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


Groups > comp.soft-sys.math.mathematica > #14346 > unrolled thread

sorting problem

Started byS <dsalman96@gmail.com>
First post2013-04-28 04:57 +0000
Last post2013-04-30 08:17 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  sorting problem S <dsalman96@gmail.com> - 2013-04-28 04:57 +0000
    Re: sorting problem Sid <pcoords29@gmail.com> - 2013-04-30 08:17 +0000

#14346 — sorting problem

FromS <dsalman96@gmail.com>
Date2013-04-28 04:57 +0000
Subjectsorting problem
Message-ID<klia8j$s8j$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#14360

FromSid <pcoords29@gmail.com>
Date2013-04-30 08:17 +0000
Message-ID<klnunf$g39$1@smc.vnet.net>
In reply to#14346
On Apr 28, 8:57 am, S <dsalma...@gmail.com> wrote:
> 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:

Hi,

How about
                   Ordering@N@distances

 HTH

Cheers
  Sid




> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web