Index a list - and number the list

Page 1 of 1

1 Replies - 1310 Views - Last Post: 12 May 2009 - 06:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=104617&amp;s=87f6e5e791c5ced5abf58049abfec533&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 tgruen

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

Index a list - and number the list

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

Reputation: 3
• 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