1 Replies - 1524 Views - Last Post: 12 May 2009 - 06:32 AM Rate Topic: -----

#1 tgruen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-May 09

Index a list - and number the list

Post icon  Posted 11 May 2009 - 03:55 PM

Howdy everyone. I would imagine this is not a tough task but I cannot find information on exactly what I need to accomplish.

Let's say I have a table with fruit. Field names fruitid and fruitname.

I query select * from fruit order by fruitname. Then I take this data and create an array with the fruitnames.

What I want to do is is list the results with their place in the list/array. Not their fruitid. For example:

1 Apple
2 Orange
3 Pear

Where the 1,2,3 is the order of the list and not any data retrieved form the query.

I have been poking around in the online help, CFWACK and finding information on indexed lists is eluding me. I was looking at listgetat but the results will be different every time and it did not seem like the solution.

Any thoughts? Any help in a new direction is welcome.

Is This A Good Question/Topic? 0
  • +

Replies To: Index a list - and number the list

#2 fremgenc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 119
  • Joined: 15-November 07

Re: Index a list - and number the list

Posted 12 May 2009 - 06:32 AM

A simple but not too efficient method:

Convert the list into an array, call it Array1
Then make another array, Array2, from Array1 but remove the numbers at the beginning of each element.

pseudo code:
foreach element in Array1
   <cfset Array2[i] =  right(Array1[i], indexOf(Array1[i]," ") />
<!---So basically take what ever is to the right of the space, you might have to use left() or mid() string functions--->

Now make your own sorting routing based on Array2 and every time you swap an element in Array2, also swap it in Array1. But when sorting, only compare using Array2.

Hope this helps!

This post has been edited by fremgenc: 12 May 2009 - 06:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1