Just to spawn a little discussion here.
The issue of sorting 2D Array's has come up and
been discussed quite a bit here so I will give
my feedback on the issue.
This weekend I experimented and tinkered a bit
and came up with a few different ways of doing
this.
The 'brute force' way I came up with was to use
Three custom tags already in existence.
Since a 2D Array models a Query rather closely
EX:
Query has Rows, and Columns
2d Array has Rows and Cols
[Row1][Col1] = Name
[Row1][Col2] = Address
[Row1][Col3] = DOB
[Row1][Col4] = Phone
[Row2][Col1] = Name
[Row2][Col2] = Address
[Row2][Col3] = DOB
[Row2][Col4] = Phone
Since there is no direct way to handle an array
via the CFX API (That I am aware of...)
You can transform your 2D array into a query,
given that you are *sure* your array has the
columns equal each time.. CF does not enforce
your arrays to have the same 'columns' per
row since they are dynamic arrays. This
leaves it to the programmer to ensure the
data is ont he up and up, otherwise there
would be no sane method for sorting a
2d array.
Now with the array as a Query there is already
a tag CFX_QuerySort qhich implements Quicksort
which is going to be extremely fast.
I implemented a quicksort and on my machine
PIII 500 with 512MB of ram it sorted
32,000 random 8 character strings (all lower case)
In 170 milliseconds or.. Less than 1/5 of a second
So the sorting is going to be the least of the
overhead.
So available was everything needed to sort a
2D Array.
There are two Custom tags written by Nate Weiss
(Which I more or less wrote and then discovered
the tags *ugh*) which are caled ArrayToQuery
and QueryToArray.
So my idea was to write a wrapper tag that calls
a C++ Sorting routine and returns the data.
My concerns in doing this were the fact that
the data is being transformed like this
Array <---> Query <---> Sort Query <---> Array
So there is the assocaited over head of
Three data transformations from Array to Query back
to Array. Working with large amounts of data
that can amount to a lot of data swapping. Again
the overhead of the actual sorting is probably
the least of the processing time/overhead in
this process because quicksort does a very
minimum of data moving.
I think there is a better way so here is what I
want some discussion on :))
The trouble in implementing Quicksort in
ColdFusion is that in C/C++ you are really
just moving the pointers around and you access
the entire data set via pointers to that data
(in most cases) as this makes sorting ultra
fast.
There are only crude ways to achieve the effect
of pointers in CF so that pretty much locks out
implementing Quicksort in CF (or any really
effecient sorting in CF)
Another problem is the fact that CF is a Typeless
language meaning a variable *can* be anything at
any given time. So when something such as a
comparison occurs
foo LT bar
The interpreter has to know what kind of variable
it is somewhere or it has to track its type
or "guess" the type any time you do anything
typeish specific with it such as a struct operation
for example. Which all leads to more overhead
which makes writing your own sorting routines in
CF less and less attractive.
So my question is.. does anyone have a better
idea for sorting a 2D array?
The only place I truly see this leading is to
avoid using Structs and Arrays that need to
be sorted when possible.
Jeremy Allen
[EMAIL PROTECTED]
[Insert Quarter]
------------------------------------------------------------------------------
Archives: http://www.mail-archive.com/[email protected]/
To Unsubscribe visit
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.