Forum

    • Register
    • Login
    • Search
    • Categories

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

    Bug Reports
    4
    19
    235
    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

                I can't help pressing F5 in the forum...

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

                  @ustk said in Array.sort() doesn't work for string arrays:

                  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

                  this never worked for me and still doesnt...

                  HISE Development for hire.
                  www.channelrobot.com

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

                    @Lindon I made a video about sorting arrays, might be of help

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

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

                      @d-healey said in Array.sort() doesn't work for string arrays:

                      @Lindon I made a video about sorting arrays, might be of help

                      oh I can sort array no problem - but it would be nice if this was available..

                      HISE Development for hire.
                      www.channelrobot.com

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

                        @Lindon Hmmm that's very strange because I am using currently everywhere in the current project and it works like a charm, at least for strings..

                        Although for objects, I've made a confusion and should've been more precise... It is not sorting json objects, but object reference names like Object 0x65de8a18 that are treated as string, which is of no use... 🙄

                        I make my own functions for sorting json, and it's just a real pain every single time... I wish there was a straight solution for this task...

                        I can't help pressing F5 in the forum...

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

                          @ustk can you give me an example of your usage?

                          HISE Development for hire.
                          www.channelrobot.com

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

                            @Lindon

                            var stringArray = ["f", "a", "z"];
                            stringArray.sortNatural(); // ["a", "f", "z"]
                            

                            I can't help pressing F5 in the forum...

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

                              @ustk hmm, yeah I get hanging update errors...dont worry about it I will continue with my home grown sort..

                              HISE Development for hire.
                              www.channelrobot.com

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

                                @Lindon said in Array.sort() doesn't work for string arrays:

                                @ustk hmm, yeah I get hanging update errors...

                                what do you mean?

                                I can't help pressing F5 in the forum...

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

                                  @ustk oh sorry wrong terms : here:

                                  MELODICSO1:! dangling listener

                                  HISE Development for hire.
                                  www.channelrobot.com

                                  ustk d.healey 2 Replies Last reply Reply Quote 0
                                  • ustk
                                    ustk @Lindon last edited by

                                    @Lindon You just buried me a bit more 🤣

                                    I can't help pressing F5 in the forum...

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

                                      @ustk said in Array.sort() doesn't work for string arrays:

                                      @Lindon You just buried me a bit more 🤣

                                      yeah I think its just not cleaning up after itself correctly..

                                      HISE Development for hire.
                                      www.channelrobot.com

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

                                        @Lindon said in Array.sort() doesn't work for string arrays:

                                        @ustk oh sorry wrong terms : here:

                                        MELODICSO1:! dangling listener

                                        What are you drinking... 😛

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

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

                                          @d-healey - the coolaid....

                                          HISE Development for hire.
                                          www.channelrobot.com

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

                                          23
                                          Online

                                          1.1k
                                          Users

                                          6.7k
                                          Topics

                                          62.1k
                                          Posts