-
Sorting a Collection
I am having a problem with a Collection. I have created a card class that holds the number (1-13) and a suit (C, D, H, S). I then have created a collection that holds 5 card objects. What I am attempting to do is sort the collection by card.number from lowest to highest.
Ex: 3H, 9C, 2D, 5S, 3C
To
2D, 3H, 3C, 5S, 9C
I have tried using multidimentional arrays, and collections. I would like to use something along the lines I use with a single dimensional array:
Array.Sort(myArray)
Unfortunatly this method dosent work with collections or multidimentional arrays. If you have a solution on how I should approach this I would really appreciat it.
-Thanks
-
You need to write your own sort method. There are several methods to do so: Quick Sort, Bubble Sort, Insertion Sort, etc... find the one that best fits your situation and implement it in your collection.
Alternatively, you can make sure that when you add items to your collection that they are inserted in the correct location - so the sorting is done when items are added.
-
Hi,
Create a single dimension string array; enter the values required, BUT it is easier if you accord letters instead of numbers to the card values and separate the value from the suit by a space
e.g. 3 hearts is C H: Jack Clubs is K C
Then store the value in a single dimension string array as sort as normal.
I
-
Very Clever solutions, thanks guys.