StringListSort
wooterslw
25 Feb 2020, 12:28How do you use this? There is no example in the help section. When I just enter it it makes it a Call Function and then errors out with:
Function not found: 'StringListSort'
Do I have to make a new List and sort the old list into it?
mrangel
25 Feb 2020, 14:08How are you using it?
As far as I can see, it should be simple enough. A single parameter which is a stringlist, and it returns a sorted version of the list.
So something like:
sortedlist = StringListSort (originallist)
wooterslw
25 Feb 2020, 14:14That's what I thought. I haven't tried that yet. Seems counterproductive. But, I'll try that when I get home. I was just doing StringListSort(StringList) thinking it would just sort the items within the stringlist.
mrangel
25 Feb 2020, 16:36Sadly, no.
There are various situations where it might be helpful to sort a list in place. For those, you would want to use a function like this:
<function name="StringListSortInPlace" parameters="lst">
sorted = StringListSort (lst)
foreach (i, sorted) {
list add (lst, i)
list remove (lst, i)
}
</function>
(it looks weird, but it should work)
Slight tangent; maybe some useful code
One algorithm I've occasionally found useful is:
<function name="ListSort" parameters="lst, expr">
stop = 0
while (ListCount (lst) > stop + 1) {
lowest = ListItem (lst, ListCount (lst)-1)
stop = ListCount (lst)
for (i, ListCount (lst) - 2, stop, -1) {
item = ListItem (lst, i)
if (Eval (expr, QuickParams ("a", item, "b", lowest))) {
list add (lst, item)
list remove (lst, item)
stop = i + 1
}
else {
lowest = item
}
}
}
</function>
This sorts a list without returning a new list. Its second parameter is an expression which should be true if item a should be placed after item b in the list.
For example, you might write ListSort (somevariable, "a > b")
to put a list of numbers into order. The same sort function can be used with any expression (possibly a function you've written) to sort any kind of list. For example, you could sort a list of NPCs into order based on some function of their stats by doing ListSort (competitors, "CalculatePowerLevel (a) > CalculatePowerLevel (b)")
.