Odd listSort() behavior

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
bmceldowney
Kobold
Posts: 8
Joined: Mon Jun 28, 2010 9:35 pm

Odd listSort() behavior

Post by bmceldowney »

I have a list!

Code: Select all

"80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011"
I would like to sort this list. When I do this, the list is sorted a in a confusing (to me) manner:

Code: Select all

"110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011, 80018, 90316"
I would expect that those last three elements would come first in a sorted list, as opposed to last. Is this a bug or are my expectations a little off?

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Odd listSort() behavior

Post by Azhrei »

Have you checked the documentation at Wiki: listSort()? There is a parameter which tells the function whether to perform a numeric sort or an alphabetic sort. Yours is an alphabetic sort (hence the one starting with "9" comes after the ones starting with "1").

bmceldowney
Kobold
Posts: 8
Joined: Mon Jun 28, 2010 9:35 pm

Re: Odd listSort() behavior

Post by bmceldowney »

Azhrei wrote:Have you checked the documentation at Wiki: listSort()? There is a parameter which tells the function whether to perform a numeric sort or an alphabetic sort. Yours is an alphabetic sort (hence the one starting with "9" comes after the ones starting with "1").
I did forget to mention in the original post (very sorry) that the result is identical regardless of whether or not I am using the "A" or "N" switch.

I would have thought that "N" would have sorted it out but apparently that is not the case.

User avatar
Azhrei
Site Admin
Posts: 12086
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: Odd listSort() behavior

Post by Azhrei »

Can you show us the pertinent code block? Using "N+" as the second parameter is the correct solution, so maybe there's something else...

bmceldowney
Kobold
Posts: 8
Joined: Mon Jun 28, 2010 9:35 pm

Re: Odd listSort() behavior

Post by bmceldowney »

Azhrei wrote:Can you show us the pertinent code block? Using "N+" as the second parameter is the correct solution, so maybe there's something else...
Thanks for the quick response!

Honestly it's nothing more complex than the following:

Code: Select all

[r:listSort("80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011", "N+")]
That gives me:

Code: Select all

110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011, 80018, 90316

User avatar
aku
Dragon
Posts: 856
Joined: Wed Nov 15, 2006 9:03 am
Contact:

Re: Odd listSort() behavior

Post by aku »

is this an issue with the # of characters?

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Odd listSort() behavior

Post by prestidigitator »

aku wrote:is this an issue with the # of characters?
I've come to the same thought from reading the Wiki: listSort() wiki page. Apparently the number is padded to 4 digits rather than actually being converted to a BigDecimal or something. That implies to me that the function will not work if there are any numbers over 9999 in the list. :roll:

You might have to roll your own. I've got a Min-Heap Library you could use, but you'd have to wrap each number in a JSON object (as the 'key' field) before putting it in the Min-Heap, then pop them off in order. Hmm. Maybe I'll write up a quick version that acts on numbers alone....
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Odd listSort() behavior

Post by prestidigitator »

Just completed a purely numeric version of the Min-Heap Library (see that thread). Here's some test code:

Code: Select all

[h: list = json.fromList("80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011")]

[h: heap = NumMinHeap.new(list)]

[h: sortedList = ""]
[h, while (json.length(heap) > 0), code:
{
   [h: sortedList = listAppend(sortedList, NumMinHeap.get(heap))]
   [h: heap = NumMinHeap.pop(heap)]
}]

[r: sortedList]
and here are the results:

Code: Select all

50011, 80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515
The library tests each value added to make sure it is, in fact, a number. It may not be the most efficient implementation of heap sort ever (especially because its implemented in macro code rather than a single Java API call), but it'll do what you want and at least do it in O(N log N) time and O(N) space even if the constant multiplier is pretty high.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Odd listSort() behavior

Post by aliasmask »

I've been trying to get away from the lists in general when I can use a json in its place. Now when you create your list you do it as a json of number, then you wouldn't have to worry about converting the text to numbers:

Code: Select all

[H: list = "80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011"]
[H: jList = ""]
[H, C(listCount(list)): jList = json.append(jList,number(listGet(list,roll.count)))]
[H: jListSorted = json.sort(jList,"a")]
[R: json.toList(jListSorted,"<br>")]

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Odd listSort() behavior

Post by prestidigitator »

aliasmask wrote:I've been trying to get away from the lists in general when I can use a json in its place. Now when you create your list you do it as a json of number, then you wouldn't have to worry about converting the text to numbers....
Oh! Oops! I completely missed Wiki: json.sort()! That makes my numeric min-heap kinda superfluous, at least for a one-shot sort algorithm. :oops: Oh well.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

prestidigitator
Dragon
Posts: 317
Joined: Fri Apr 23, 2010 8:17 pm

Re: Odd listSort() behavior

Post by prestidigitator »

Yeah, this works just fine:

Code: Select all

[h: list = json.fromList("80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011")]
[h: sortedList = json.sort(list)]
[r: json.toList(sortedList)]
And produces the output:

Code: Select all

50011,80018,90316,110014,130015,200016,220013,270016,360016,440516,440816,472515
which is the same as my heap sort, but without the extra whitespace (funny how Wiki: json.toList() and Wiki: listAppend() behave differently there) and is much, much faster.
"He knows not how to know who knows not also how to un-know." --Sir Richard Burton

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Odd listSort() behavior

Post by aliasmask »

One of the reasons I like JSONs better is because of the built in functions they have, like Wiki: json.difference(), json.merge, json.union and json.intersection. It make manipulating lists pretty easy.

One trick I like for getting rid of empty list items is:

Code: Select all

[noEmptyItems = json.difference(bigList,json.append("",""))]
Basically, you subtract the second list from the first list.

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Odd listSort() behavior

Post by aliasmask »

prestidigitator wrote:Yeah, this works just fine:

Code: Select all

[h: list = json.fromList("80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011")]
[h: sortedList = json.sort(list)]
[r: json.toList(sortedList)]
And produces the output:

Code: Select all

50011,80018,90316,110014,130015,200016,220013,270016,360016,440516,440816,472515
which is the same as my heap sort, but without the extra whitespace (funny how Wiki: json.toList() and Wiki: listAppend() behave differently there) and is much, much faster.
Odd, I tried that first and it still sorted them as a string. Ahhh... I see, I did this:

Code: Select all

[H: jList = json.sort(json.fromList("80018, 90316, 110014, 130015, 200016, 220013, 270016, 360016, 440516, 440816, 472515, 50011"),"a")]
[R: jList]
But when I separate the json.sort like you did, then it sees them as numbers.

bmceldowney
Kobold
Posts: 8
Joined: Mon Jun 28, 2010 9:35 pm

Re: Odd listSort() behavior

Post by bmceldowney »

Wow, thanks for the feedback!

Now that we've been through all of that, I have completely changed my implementation of the feature, making this a moot point. I like the advice about the json arrays though. Is there a performance impact to using jsons over string lists?

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: Odd listSort() behavior

Post by aliasmask »

I think lists and json array are handled internally virtually the same way, so I don't think so. But I do know for most things where a list will work, so will a json array. You can do more stuff with a json and don't have to worry about special characters (like commas in your list).

Post Reply

Return to “Macros”