Comparators in CF with Groovy

I don't know about anyone else, but wanting to create little ad hoc Java classes in my CF apps in a common occurrence for me.  With my CF Groovy project, it's both possible and very easy.  No Java, no compiling, no classloading hacks.

Here's a simple example.  I'm going to create an array of structs and then sort the array based on a specific field value (the 'date' field) of the structs.  Doing this in CF usually means making a lookup struct, sorting that, and then rebuilding the array (but only if you have unique values), or converting to a query and using QofQ to do it.  Both have serious drawbacks, as well as being very circuitous/obscuring.

First the array construction (six items, each with 'letter' and 'date' fields, holding what you'd expect).  Nothing very interesting here.

<cfscript>
a = [];
month = createDate(year(now()), month(now()), 1);
for (i = 1; i LTE 6; i = i + 1) {
    s = {
        letter = chr(65 + randRange(0, 25)),
        date = dateAdd("d", randRange(0, 30), month)
    };
    arrayAppend(a, s);
}
</cfscript>

Now, using the <g:script> tag from CF Groovy, we'll sort it by date using a java.util.Comparator:

<g:script>
Collections.sort(variables.a, {o1, o2 ->
    o1.date.compareTo(o2.date)
} as Comparator)
</g:script>

If you want to compare based on the letter, just change the two ".date" references to ".letter".  You can, of course, make the comparator as complicated as you'd like, including referencing other Groovy classes through CF Groovy's classloading, or other Java classes on your classpath.

You can see it in action at the demo page, or get the full source from Subversion at https://ssl.barneyb.com/svn/barneyb/cfgroovy/trunk/demo/.

6 responses to “Comparators in CF with Groovy”

  1. Kai Tischler

    Hello dear Barney !

    I for one have very much interest in Your "CF with Groovy" efforts :-) ! When will You make it publicly available with enough documentation to get us all up to speed :-) ?

    Until now I have been a ColdFusion purist; but recently I have come to think that leveraging Java – e.g. for the domain modeling – could be in one's best interest …

    So one has several choices:
    - Leaving the ColdFusion camp completely and perhaps enter the Grails/Groovy community
    - Still use ColdFusion where it is appropriate and leverage Your "CF with Groovy" micro framework where it is appropriate

    The questions are then:
    - What can ColdFusion still do better than Grails/Groovy ? The following items come to mind immediately: Event gateways; CFCs which can be configured to return JSON, XML, or web services; existing CFC functionality; tags like cfoutput …
    - Which CF framework do You recommend for leveraging Your micro framework "CF with Groovy" to its fullest potential ?
    - As somebody who has NOT been in the ColdFusion/Java/Grails/Groovy trenches himself, it may be difficult to judge in which situation to leverage which approach; so yes: some guidelines/explanations with regard to that complex topic could be valuable, too :-) !

    I am looking forward to "CF with Groovy" :-) !

    Best Regards and Tschüss

    Kai

  2. Scriptlets in CF Anyone? at BarneyBlog

    [...] Contact « Comparators in CF with Groovy [...]

  3. Railo 3 Beta 2 at BarneyBlog

    [...] and java.util.Map respectively, greatly easing Java integration.  This also allows the Comparators CF Groovy example I published to work correctly on [...]

  4. Aja Woods

    Awesome!! Thanks this was a great help in sorting lists in my grails app!! Thanks again.

  5. Jeff

    Thanks for the example!