HISE Logo Forum
    • Categories
    • Register
    • Login

    How to sort through multiple array?

    Scheduled Pinned Locked Moved Scripting
    4 Posts 3 Posters 271 Views
    Loading More Posts
    • 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.
    • G
      Gudru
      last edited by

      Hello,

      I have a situation where I have two arrays that are associated together

      reg array1 = ["apples", "oranges", "bananas"];
      reg array2 = ["red", "orange", "yellow"];
      

      if I sort one:

      array1.sortNatural(); //apples, bananas, oranges
      

      then I cannot use index to associate array 2 with array1, since array1 has been sorted, whereas array2 has not been sorted. How do I make it so that when array1 is sorted, array2 gets sorted into the same places as array1 is so the data stay together?

      Thank you!

      A LindonL 2 Replies Last reply Reply Quote 0
      • A
        aaronventure @Gudru
        last edited by

        @Gudru

        You'll need to write your own array sorting function, and have it take an additional array (your array2) and apply the same indexing as for array 1.

        There are plenty of array sorting algorithms. You might care about whether it's stable (whether the equal elements maintain their original indexes), how it performs for small vs. large arrays etc.

        1 Reply Last reply Reply Quote 0
        • LindonL
          Lindon @Gudru
          last edited by

          @Gudru said in How to sort through multiple array?:

          Hello,

          I have a situation where I have two arrays that are associated together

          reg array1 = ["apples", "oranges", "bananas"];
          reg array2 = ["red", "orange", "yellow"];
          

          if I sort one:

          array1.sortNatural(); //apples, bananas, oranges
          

          then I cannot use index to associate array 2 with array1, since array1 has been sorted, whereas array2 has not been sorted. How do I make it so that when array1 is sorted, array2 gets sorted into the same places as array1 is so the data stay together?

          Thank you!

          use objects, and sort an array of them...

          reg myArray = [ {"Fruit": "apples", "Colour" : "red"},
                          {"Fruit": "oranges", "Colour" : "orange"},
                          {"Fruit": "bananas", "Colour" : "yellow"}
                        ];
          

          HISE Development for hire.
          www.channelrobot.com

          LindonL 1 Reply Last reply Reply Quote 1
          • LindonL
            Lindon @Lindon
            last edited by

            and if you havent used objects in an array before here is how to reference them

            for (f in myArray)
            {
                  Console.print("my fruit is:" + f.Fruit);
                  Console.print("its colour is:" + f.Colour);
            
            }
            

            HISE Development for hire.
            www.channelrobot.com

            1 Reply Last reply Reply Quote 1
            • First post
              Last post

            48

            Online

            1.7k

            Users

            11.7k

            Topics

            101.8k

            Posts