Today's Friday Puzzler should be one of the simpler ones I've posted. While having a random Facebook discussion (as if there are any other sort), the topic of sorting came up. I thought it might be an interesting challenge to write code that determines if a list of data is sorted and report back on what type of sort was being used. While there are probably more, I can think of the following sort types:
- case sensitive text ascending
- case sensitive text descending
- case insensitive text ascending
- case insensitive text descending
- numeric ascending
- numeric descending
And of course, it is possible there is no sort applied at all. Your goal is to take an input and report back the sort type. To make it easier for you, I've created a script that generates an array of inputs you can use for - well, inputs. I'm also looking for folks to try their hand at this in JavaScript. So to make that easier, I used the ColdFusion toScript function (y'all know about that, right?) to output JavaScript you can cut and paste.
Enjoy!
Archived Comments
http://pastebin.com/YGeczdqz
Woot. Someone tried it. :) Now I'm going to get picky.
isSorted: The name bugs me as it implies a boolean result. I'd have maybe gone with getSortType.
I find it interested you check for multiple sort types and return them all. I didn't expect that, and that's kinda cool.
Frankly, your solution is perfect. I swear - I didn't even think of just doing a compare. Awesome.
Finally, damn good job on handling the 1 item and empty length items. I forgot those in my test inputs. Smart.
Finally (2) - you sure you need that if? You can still list sort a 1 character string. It's dumb, but you can.
Yeah, the function name isn't the best for the reason you mentioned.
Good point on the extra cases that were added. I guess sorting a one element list didn't seem right so I didn't think to just let it go through the normal process.
I'd like to see what solutions people come up with for large datasets...
Heh, well Russ, maybe folks will chime in. I normally get a bit more participation on these puzzlers, but I forgot it was Good Friday. I'm thinking many people are off today.
(~?~)-zzZ
JavaScript:
http://pastebin.com/GK1RfEy0
ColdFusion:
http://pastebin.com/euF4eRh6
Isn't this a bug?
I just ran this with ColdFusion 10 with Update 8.
<cfscript>
s = "c,C,b,B,a,A";
desc = listSort(s, "textnocase", "desc");
writeoutput(desc);
</cfscript>
The result should be the same as the value of "s" instead of "C,c,B,b,A,a."
"textnocase" with "asc" works just fine though.
I guess it's not a bug.
Here is from the official documentation.
http://help.adobe.com/en_US...
For example, in a textnocase, desc sort of d,a,a,b,A, the following occurs:
ColdFusion MX returns d,b,A,a,a
Earlier ColdFusion releases return d,b,a,a,A
Weird. It's not case-insensitive then.
arraySort with "textnocase" and "desc" too.
Do you know why?
I disagree. It is case-insensitive in that the "A"s show up in their proper place. They are after the d. But internally, they happen to be sorted themselves, and that changed in MX.
Here is my JS solution: https://gist.github.com/rus...
It is written in FP-style using Underscore.js and should work well for large datasets. Let me know what you think!
This should work better than my previous one.
http://pastebin.com/vQ5dqbZ3
Fascinating Russ and AXL. Thank you for sharing these.