Forum

    • Register
    • Login
    • Search
    • Categories

    Array.sort() doesn't work for string arrays

    Bug Reports
    3
    6
    123
    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.
    • Casey Kolb
      Casey Kolb last edited by

      Just noticed this bug. Array.sort() will only work on numbers and not strings. Can others confirm this happens in their HISE builds?

      var letters = ["d", "e", "c", "b", "a"];
      letters.sort();
      
      for (l in letters)
      	Console.print(l);
      // prints d e c b a
      
      var numbers = [3, 2, 1, 5, 4];
      numbers.sort();
      
      for (n in numbers)
      	Console.print(n);
      // prints 1 2 3 4 5
      

      Casey Kolb
      Founder & CEO of Lunacy Audio
      Composer | Producer | Software Developer

      1 Reply Last reply Reply Quote 0
      • d.healey
        d.healey last edited by

        I don't think it's a bug, but it would be nice to have it sort strings too.

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        1 Reply Last reply Reply Quote 0
        • Casey Kolb
          Casey Kolb last edited by

          Based on the documentation it is: Array.sort()

          Casey Kolb
          Founder & CEO of Lunacy Audio
          Composer | Producer | Software Developer

          d.healey 1 Reply Last reply Reply Quote 0
          • d.healey
            d.healey @Casey Kolb last edited by

            @Lunacy-Audio yep, you're right

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            1 Reply Last reply Reply Quote 0
            • Casey Kolb
              Casey Kolb last edited by

              For the time being, I just pulled a string sort algorithm from the web for sorting string arrays. Works great for now 😀

              inline function sortStringArray(str) { 
                  local i = 0;
                  local j;
                  while (i < str.length) { 
                      j = i + 1; 
                      while (j < str.length) { 
                          if (str[j] < str[i]) { 
                              local temp = str[i]; 
                              str[i] = str[j]; 
                              str[j] = temp; 
                          } 
                          j++; 
                      } 
                      i++; 
                  } 
              } 
              

              Casey Kolb
              Founder & CEO of Lunacy Audio
              Composer | Producer | Software Developer

              1 Reply Last reply Reply Quote 1
              • ustk
                ustk last edited by

                I've made a pull request after adding Array.sortNatural() API.
                It allows sorting strings, conventional numbers, strings containing numbers with a number priority, and objects (treated as string object name).
                You can also sort a mix of all the types

                Tired to press F5 in the forum...
                Studio427 Audio - Audio Instruments & FX Plugins for music production. Website - Facebook

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

                7
                Online

                857
                Users

                5.7k
                Topics

                53.1k
                Posts