Friday Puzzler: Sort Type

This post is more than 2 years old.

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.


Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Shawn posted on 3/29/2013 at 11:59 PM
Comment 2 by Raymond Camden posted on 3/30/2013 at 12:11 AM

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.

Comment 3 by Shawn posted on 3/30/2013 at 12:18 AM

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.

Comment 4 by Russ posted on 3/30/2013 at 1:19 AM

I'd like to see what solutions people come up with for large datasets...

Comment 5 by Raymond Camden posted on 3/30/2013 at 1:22 AM

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.

Comment 6 by AXL posted on 3/30/2013 at 10:11 AM
Comment 7 by AXL posted on 3/30/2013 at 11:11 AM

Isn't this a bug?
I just ran this with ColdFusion 10 with Update 8.

s = "c,C,b,B,a,A";
desc = listSort(s, "textnocase", "desc");

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.

Comment 8 by AXL posted on 3/30/2013 at 11:34 AM

I guess it's not a bug.

Here is from the official documentation.

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?

Comment 9 by Raymond Camden posted on 3/30/2013 at 5:25 PM

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.

Comment 10 by Russ posted on 3/31/2013 at 12:17 AM

Here is my JS solution:

It is written in FP-style using Underscore.js and should work well for large datasets. Let me know what you think!

Comment 11 by AXL posted on 3/31/2013 at 10:19 PM

This should work better than my previous one.

Comment 12 by Raymond Camden posted on 4/1/2013 at 6:09 AM

Fascinating Russ and AXL. Thank you for sharing these.