Skip to content

ArrayList - adding keys???

Simon

Asked Feb 16, 2006, 3:14 AM EST1 answer
Share
LinkedInX

I have an Arraylist of objects - but I want to attach to each object a number (in this case distance of type int).  This will be useful for me to sort on the distance value, the shortest to the longest and then do other operations.

eg.

OBJECT       | INDEX | DISTANCE
-------------------------------
fooOject        [0]     100
fooOject        [1]     1465
fooOject        [2]     8787
fooOject        [3]     21
fooOject        [4]     66....etc


Does anyone know how to do this, bearing in mind Im a bit of C# newbie...

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Like you said, you have an arraylist of "objects", why not have custom objects
    have a class like
    class myclass {
       int sortkey;
       string myVal;
    }
    now your array list will have instances of this class ( you create a new instance, set the values and put it into your arraylist)

    once you have this, you write your own sort() mthod that uses sortkey member in your class and sort based on that....

    too much? what you think ;-)

    Cheers!
    0