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 -
@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 typesthis never worked for me and still doesnt...
-
@Lindon I made a video about sorting arrays, might be of help
-
@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..
-
@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...
-
@ustk can you give me an example of your usage?
-
var stringArray = ["f", "a", "z"]; stringArray.sortNatural(); // ["a", "f", "z"]
-
@ustk hmm, yeah I get hanging update errors...dont worry about it I will continue with my home grown sort..
-
@Lindon said in Array.sort() doesn't work for string arrays:
@ustk hmm, yeah I get hanging update errors...
what do you mean?
-
@ustk oh sorry wrong terms : here:
MELODICSO1:! dangling listener
-
@Lindon You just buried me a bit more
-
@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..
-
@Lindon said in Array.sort() doesn't work for string arrays:
@ustk oh sorry wrong terms : here:
MELODICSO1:! dangling listener
What are you drinking... :p
-
@d-healey - the coolaid....
-
What is the Meaning of This?
dangling listener
It happens once I Check the plugin Parameters Names using Tools / Validate Plugin Parameters!