Click to See Complete Forum and Search --> : multiple index on an array


Anders Eriksson
May 27th, 1999, 09:25 AM
I have an 'array' of objects that I need to have indexed in 2 ways, i.e. I need to be able to loop thru the array in 2 different orders. I don't want to create 2 arrays and sort them differently. How do I solve this?

///Anders

Jim Watters
May 27th, 1999, 10:11 AM
for the second index try using an array of pointer to the original data. Have it sorted based on what it points to.

May 27th, 1999, 10:13 AM
Can't be done without a second array or an ugly function that would really slow down response. I have always handled it with a second array of pointers to the objects - this way I don't use up much memory or increase allocations. Since you don't have to manage the pointers - there should be no problems with cleaning up.

ChrisD
May 27th, 1999, 10:20 AM
I tend to use a class derived from the base array class and overiding all the functions for add/remove/get and add a second array as a member of this new class, and keep it up to date during all add/deletes. I then have two GetAt functions
GetAtSort1 / GetAtSort2, and I also have a function that says set sort mode so that the generic GetAt / operator[] return the correctly sorted array entry.


HTH,
Chris