Array.sort() doesn't work for string arrays
-
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
-
I don't think it's a bug, but it would be nice to have it sort strings too.
-
Based on the documentation it is: Array.sort()
-
@Lunacy-Audio yep, you're right
-
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++; } }
-
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