Results 1 to 4 of 4

Thread: Sorting a Collection

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    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

  2. #2
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    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.

  3. #3
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Very Clever solutions, thanks guys.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width