Sorting 2-Dimensional Arrays

There was a post on the BACFUG mailing list yesterday which I think makes for a good tip. Someone was asking how to use the arraySort function to sort two-dimensional arrays, which it does not support. The response was a recommendation to create a query instead, then use query of query to get the record set in the correct order. What would you suggest?

7 Responses to Sorting 2-Dimensional Arrays

  1. Matt Liotta says:

    How about using a UDF from Seems like would do the trick.

  2. Sam says:

    I would suggest using either a query or an array of structures depending on the situation. A 2D array is almost never useful in programming and the second dimension (or even worse the first) is just an association between an index and a specific key. Programming in 2D arrays is far more time consuming and bug-prone when compared to programming a 1D array of structures or a query.There are cases where a 2D array is useful, such as when really representing a grid of data where the columns themselves are not meaningful as named fields and only as indexed columns. This is a very rare case.

  3. Scott Keene says:

    In other languages, it is a well known fact that the “QuickSort” algorithm is the most efficient method of sorting , including 2D arrays. Essentially, it employs a “divide and conquer” approach by choosing one element then shifting the other elements so that everything less than the chosen element comes before it in the array, and everything greater than the partition element comes after it . Then it sorts each half by calling itself 2x (recursive function). I’d recommend looking into it at least, especially if the array will be reasonably large. You could google it and find tons of reference I’m sure.I haven’t seen the UDF referenced above, but it would be cool to see a QuickSort implementation in CF – although I suspect you could find a Java class that handles the job nicely.

  4. Scott Keene says:

    Sorry for the double post, but that first line should have read “…most efficient method of sorting arrays, including 2D arrays.” I forgot the first “arrays”, I didn’t mean to imply QS was the best method of sorting across the board, just 1 and 2D arrays.

  5. Scott Keene says:

    As a follow up and to completely be a nuisance, the java.utils.Arrays class has numerous overloaded sort() methods for sorting arrays and implements the QuickSort algorithm. It would take two lines of code to accomplish this with Java, first line to create the Java object, second to call the sort() method on your CF array. Bang.Big problem though: can’t pass an array to Java!! At least I haven’t figured out how to do this yet. JavaCast() can’t cast an array and I’ve found no workaround. Anyone?Here’s a link to the Sun docs on java.util.Arrays:

  6. PaulH says:

    scott, a heavy duty javacast for these kinds of situtations is really needed. i’ve added it to the cf wishlist (public & beta), maybe you could shout at mm too?i’ve passed arrays to java before, though if the array is manipulated in place, cf will never see it. cf arrays are mapped to lists in java & the java will actually operate on a copy of that (as an array). a couple of lines of wrapper class will workaround that.

  7. I ran into this problem some hours ago. however, i will give thumbs up for the arraysort2d at cflib. if the wheel turns, why reinvent it?