How to do a custom sort?

X

Xenosis

Guest
I have a words list which is already sorted alphabetically
I want to be able to read this in from the included files and then sort it based on word length but i don't see anyway with ds_lists for instance because the sort is just alphabetical

Is there a way to write a custom comparator in Gamemaker so i can sort based on word length?
 

Yal

šŸ§ *penguin noises*
GMC Elder
I have a words list which is already sorted alphabetically
I want to be able to read this in from the included files and then sort it based on word length but i don't see anyway with ds_lists for instance because the sort is just alphabetical

Is there a way to write a custom comparator in Gamemaker so i can sort based on word length?
I think the easiest way to do this would be using a priority queue data structure: go through the word list, add everything to the priority queue with the string length as the priority, and then keep removing the highest (or lowest, depending on what sort order you want) priority element and putting it into a new array/ds_list.
 
X

Xenosis

Guest
You can implement any sorting algorithm you want, it's pretty basic comp-sci (I mean that as in it's not too difficult to do if you put your mind to it, not that you're stupid or anything like that): https://en.wikipedia.org/wiki/Sorting_algorithm
Yeah but why reinvent the wheel, this should be part of the core language

I did a one liner in the shell and exported the data already sorted by word length to make up for the inadequacies of GML not being able to do something so basic
 
Top