Posts Tagged ‘Arrays’

Ordering an array of Objects

Monday, November 15th, 2010

So we all know that if we have an array of simple values sorting that array according to those values in Coldfusion couldn’t be easier. Using the arraySort() function will handle sorting the array fairly effortlessly on the developer’s part. However, what if you have an array of objects. Each object in the array contains a value that you want to sort by. Here things get slightly more complicated as the arraySort() function will not work in this case. However, sorting the array is still fairly simple. Before getting into this, let’s take a quick look at an array containing a set of objects with index values that need to be sorted.

Build an array with objects

<cfset variables.model = structNew() />

<cfset variables.model.data = arrayNew(1) />

<cfset variables.model.data[1] = structNew() />
<cfset variables.model.data[1].Index = 2 />

<cfset variables.model.data[2] = structNew() />
<cfset variables.model.data[2].Index = 4 />

<cfset variables.model.data[3] = structNew() />
<cfset variables.model.data[3].Index = 1 />

<cfset variables.model.data[4] = structNew() />
<cfset variables.model.data[4].Index = 3 />

This produces the following visually:

unordered array

Unordered Array of Objects

(more…)

Using the structNew() and structClear() functions

Monday, November 15th, 2010

While the structNew() and structClear() functions do relatively the same thing, one thing to keep in mind is that the structNew() function creates a new struct and the structClear() function clears a single existing struct and all of its instances. While this may seem obvious, the last part mentioned (all existing instances of a struct) can create some fairly weird scenarios if not careful.

Essentially, if a loop is created and in each loop a struct is created and stored in an array, structClear() cannot be used to refresh the struct on each loop without clearing all instances of the struct. What happens is on loop one, the struct is created and assigned a value. On loop two the struct is cleared and a new value is inserted and appended to the array. This is great except now you do not have two independent structs. Instead you have two instances of the same struct. The result is that clearing the struct not only empties both structs, but when the new value is assigned it is assigned to both structs. Now you have two structs that are identical (this is of course appropriate if all the structs should be identical and better form as they can all be updated much more efficiently). On the eighth loop you will have eight instances of the same struct.

What we want is a new struct with a different value for each loop. As such there does need to be something that resets or creates a new struct for each loop iteration. If not, you will end up with eight structs with every value assigned to them.

To do this you need to use the structNew() function as this will create a whole new struct independent of the struct created in the previous loop. This allows the variable assignment to change (in this case to a new struct) without effecting the previously stored struct. At this point only pin pointing the struct manually will allow it to be manipulated because we’re now working with a new struct in a new array index. To better understand let’s look at a couple examples of each instance.

(more…)