Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how can i get Array sort index?

how can i get Array sort index?

Scheduled Pinned Locked Moved C#
questiondatabasedata-structurestutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    smallkubi
    wrote on last edited by
    #1

    Hi, all friends: i want to get a efficient function to sort array, and return a old array index . For example: double[] a=[5,4,1,3,2]; how can i get b is [1,2,3,4,5],and index array c is [2,4,3,1,0]? i want a efficent and quick method...

    OriginalGriffO L B 3 Replies Last reply
    0
    • S smallkubi

      Hi, all friends: i want to get a efficient function to sort array, and return a old array index . For example: double[] a=[5,4,1,3,2]; how can i get b is [1,2,3,4,5],and index array c is [2,4,3,1,0]? i want a efficent and quick method...

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      There is no built in method that will do that for you - the index to the previous array is what causes the problem - and you can't "retrospectively" find it because in the event of duplicates, you don;t know which value to use. What I would probably do is package the value you want to sort into a struct together with it's original index and sort that, using a comparator that looked only at the values. The resulting sorted info would include the original array index, and it would be pretty trivial to split them back up if you needed to.

      public struct sortable
      {
      double value;
      int index;
      }

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • S smallkubi

        Hi, all friends: i want to get a efficient function to sort array, and return a old array index . For example: double[] a=[5,4,1,3,2]; how can i get b is [1,2,3,4,5],and index array c is [2,4,3,1,0]? i want a efficent and quick method...

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        `Array.Sort` has an overload that takes and array of keys and an array of values. If you sort [0,1,2,3,4] with the keys [5,4,1,3,2], it will give you [2,4,3,1,0], as you wanted. sandbox[^]

        S 1 Reply Last reply
        0
        • L Lost User

          `Array.Sort` has an overload that takes and array of keys and an array of values. If you sort [0,1,2,3,4] with the keys [5,4,1,3,2], it will give you [2,4,3,1,0], as you wanted. sandbox[^]

          S Offline
          S Offline
          smallkubi
          wrote on last edited by
          #4

          good idea,smart!!! and i would to know how to generate array 0 to 100 , not use loop?

          L 1 Reply Last reply
          0
          • S smallkubi

            good idea,smart!!! and i would to know how to generate array 0 to 100 , not use loop?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Apart from typing it out manually? Just use a loop.

            int[] index = new int[100];
            for (int i = 0; i < index.Length; i++)
            index[i] = i;

            inb4 `Enumerable.Range(0, 100).ToArray()` - oh sure, but that has the same loop, and also does a bunch of array allocations and copies. Sweeping a loop under the rug doesn't make it go away.

            S 1 Reply Last reply
            0
            • L Lost User

              Apart from typing it out manually? Just use a loop.

              int[] index = new int[100];
              for (int i = 0; i < index.Length; i++)
              index[i] = i;

              inb4 `Enumerable.Range(0, 100).ToArray()` - oh sure, but that has the same loop, and also does a bunch of array allocations and copies. Sweeping a loop under the rug doesn't make it go away.

              S Offline
              S Offline
              smallkubi
              wrote on last edited by
              #6

              OK,I SEE,thank you

              1 Reply Last reply
              0
              • S smallkubi

                Hi, all friends: i want to get a efficient function to sort array, and return a old array index . For example: double[] a=[5,4,1,3,2]; how can i get b is [1,2,3,4,5],and index array c is [2,4,3,1,0]? i want a efficent and quick method...

                B Offline
                B Offline
                BillWoodruff
                wrote on last edited by
                #7

                A little Linq, perhaps:

                int[] iary = new[] { 5, 4, 1, 3, 2 };
                var sorted = iary.OrderBy(itm1 => itm1).ToArray();
                var unsortedIndexes = sorted.Select(itm2 => Array.IndexOf(iary, itm2, 0)).ToArray();

                Note that using 'OrderBy creates a copy of the Array.

                >? sorted
                {int[5]}
                [0]: 1
                [1]: 2
                [2]: 3
                [3]: 4
                [4]: 5

                ? unsortedIndexes
                {int[5]}
                [0]: 2
                [1]: 4
                [2]: 3
                [3]: 1
                [4]: 0

                ? iary // after creating 'sorted using 'OrderBy
                {int[5]}
                [0]: 5
                [1]: 4
                [2]: 1
                [3]: 3
                [4]: 2

                «Tell me and I forget. Teach me and I remember. Involve me and I learn.» Benjamin Franklin

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups