Results 1 to 16 of 16

Thread: [RESOLVED] Arraylist doesn't sort right

  1. #1

    Thread Starter
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Resolved [RESOLVED] Arraylist doesn't sort right

    Is it possible that the arraylist doesn't sort right.

    I Inherited the arraylist to make my own list that can only contain my type of object.

    My object has its onw compare to method (i implemented Icomparable) and the compare to gives the right output, the sort doesn't go right.

    Are there any problems known with the arraylist that i should know about?

  2. #2
    Addicted Member
    Join Date
    Dec 2004
    Location
    Ohio
    Posts
    153

    Re: Arraylist doesn't sort right

    Is this what you mean?

    VB Code:
    1. Dim ary As New ArrayList
    2.         ary.Add("Sort")
    3.         ary.Add("Names")
    4.         ary.Add("Here")
    5.  
    6.         ary.Sort()
    7.  
    8.         Dim i As Int32
    9.         For i = 0 To ary.Count - 1
    10.             outLabel.Text &= ary(i) & Environment.NewLine
    11.         Next

  3. #3
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Arraylist doesn't sort right

    Does it give an error when you sort? Or does it output the wrong order? If it errors, what is the error? If the out put is wrong, what is is outputing and how should it be outputing? Also is it outputing in the order it was added, or is it different?
    i.e. Should be a,b,c,d comes out d,c,b,a

  4. #4
    Addicted Member
    Join Date
    Dec 2004
    Location
    Ohio
    Posts
    153

    Re: Arraylist doesn't sort right

    No errors here... the code below displays "abcd" in the label "outLabel".

    VB Code:
    1. Dim ary As New ArrayList
    2.         ary.Add("d")
    3.         ary.Add("b")
    4.         ary.Add("c")
    5.         ary.Add("a")
    6.  
    7.         ary.Sort()
    8.  
    9.         Dim i As Int32
    10.         For i = 0 To ary.Count - 1
    11.             outLabel.Text &= ary(i)
    12.         Next

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Arraylist doesn't sort right

    Hlstriker, Rob is doing it differently. He is attempting to sort his own objects, which means it's not neccassarily aphlabetical.

  6. #6
    Addicted Member
    Join Date
    Dec 2004
    Location
    Ohio
    Posts
    153

    Re: Arraylist doesn't sort right

    oh sorry, i'm still a noob, I thought he was asking something I was helped with earlier.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Arraylist doesn't sort right

    Still, it should sort correctly. I have used IComparable a few times, and sorted fine. On the otherhand, I was using Array.Sort rather than arraylist. Perhaps posting your IComparable comparison function would show something.
    My usual boring signature: Nothing

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Arraylist doesn't sort right

    You shouldn't be inheriting ArrayList in the first place. If you want to create a strongly-typed collection you should be inheriting CollectionBase, as the help/MSDN instructs. If you still have issues after fixing that then it is almost certainly an issue with your implementation of IComparable.CompareTo, because it's that and that alone that determines the sort order.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Arraylist doesn't sort right

    It did'nt give any errors, the output of the sort was just wrong.
    I'm trying to make a cardgame. and my array should only contain my cardItems.

    It should sort: Seven, Eight, Nine, Jack, Queen, King, Ace, Ten.
    It always sorts like this: Seven, Eight, Nine, Ace, Ten, Jack, Queen, King

    When i test the compareto method of my carditem object: it says that the Ace is bigger than the King, and The Ten is the bigges.

    -----------------------------------

    I'll try the collectionbase and see if that solves my problem.

    THanks for your help.

  10. #10
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Arraylist doesn't sort right

    When implementing IComparable there are some strict logic rules that you must adhere to. Can you post your code for the comparisons?
    I don't live here any more.

  11. #11
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Arraylist doesn't sort right

    Oh, and this may be of some help, its a case study I wrote last year for a custom playing-card sorting system. You could probably adapt it for your needs...

    http://www.vbforums.com/showpost.php...83&postcount=1
    I don't live here any more.

  12. #12

    Thread Starter
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Arraylist doesn't sort right

    - Cardsign contains values 0 to 3
    - Points contain values 0 to 5
    - Value contains value 0 to 12

    VB Code:
    1. Public Class clsCard
    2.     Inherits Object
    3.     Implements IComparable
    4.  
    5.     Public Overloads Function CompareTo(ByVal pCard As Object) As Integer Implements IComparable.CompareTo
    6.         If TypeOf pCard Is clsCard Then
    7.             Dim cCard As clsCard
    8.             cCard = pCard
    9.             If Me.Sign.CompareTo(cCard.Sign) = 0 Then
    10.                 If Me.Points = 0 And cCard.Points = 0 Then
    11.                     Return Me.Value.CompareTo(cCard.Value)
    12.                 Else
    13.                     Return Me.iCardPoint.CompareTo(cCard.Points)
    14.                 End If
    15.             Else
    16.                 Return Me.Sign.CompareTo(cCard.Sign)
    17.             End If
    18.  
    19.             cCard = Nothing
    20.         Else
    21.             Throw New ArgumentException("Object is not a Card-object.")
    22.         End If
    23.     End Function
    24. End Class

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Arraylist doesn't sort right

    There's no way that we can understand that method unless you explain what Sign, Points and Value mean and what values they have for each card. I'd also suggest that, if you haven't already, you use enumerations for at least some of those properties so that the values actually mean something. If they're cards then I'd use an enumeration for the suit and the face value.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Arraylist doesn't sort right

    Sign(suit): Clubs = 0, Diamons = 1, Hearts = 2, Spades = 3

    CardValue: Ace = 0, Two = 1, Three = 2, Four = 3, Five = 4, Six = 5, Seven = 6, Eight = 7, Nine = 8, Ten = 9, Jack = 10, Queen = 11, King = 12

    Points:
    Ace = 4, two to Nine = 0, Ten = 5, Jack = 1, Queen = 2, King = 3

    It should sort the cards:
    - first from clubs to spades
    - then from 2 to 9, J, Q, K, Ace, 10

    but it sorts like this:
    - first from clubs to spades
    - then from 2 to 9, Ace, 10, J, Q, K


    these values are allready in an enumeration. (thanks for the tip anyway)

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Arraylist doesn't sort right

    VB Code:
    1. Return Me.iCardPoint.CompareTo(cCard.Points)
    Does the Points property just return the value of the iCardPoint variable? I'm guessing that it doesn't and that that's your problem (from everything you've described it seems like the iCardPoint variable is returned by the Value property) but even if it does, by the rest of your logic that line should still read:
    VB Code:
    1. Return Me.Points.CompareTo(cCard.Points)
    Last edited by jmcilhinney; Dec 3rd, 2005 at 09:47 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member robbedaya's Avatar
    Join Date
    Jul 2002
    Location
    Belgium
    Posts
    872

    Re: Arraylist doesn't sort right

    i had to replace
    VB Code:
    1. Return Me.iCardPoint.CompareTo(cCard.Points)
    by
    VB Code:
    1. Return Me.Points.CompareTo(cCard.Points)

    iCardPoint was the local private variable containing the value, Points is the public Property that returns iCardPoint. Allthough they contain the same value and they are the same type, the comparison doesn't seem to go right.

    after i changed the line it went ok.

    Thanks for your help.

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