|
-
Dec 2nd, 2005, 04:06 PM
#1
Thread Starter
Fanatic Member
[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?
-
Dec 2nd, 2005, 04:27 PM
#2
Addicted Member
Re: Arraylist doesn't sort right
Is this what you mean?
VB Code:
Dim ary As New ArrayList
ary.Add("Sort")
ary.Add("Names")
ary.Add("Here")
ary.Sort()
Dim i As Int32
For i = 0 To ary.Count - 1
outLabel.Text &= ary(i) & Environment.NewLine
Next
-
Dec 2nd, 2005, 04:35 PM
#3
Frenzied Member
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
-
Dec 2nd, 2005, 04:40 PM
#4
Addicted Member
Re: Arraylist doesn't sort right
No errors here... the code below displays "abcd" in the label "outLabel".
VB Code:
Dim ary As New ArrayList
ary.Add("d")
ary.Add("b")
ary.Add("c")
ary.Add("a")
ary.Sort()
Dim i As Int32
For i = 0 To ary.Count - 1
outLabel.Text &= ary(i)
Next
-
Dec 2nd, 2005, 04:43 PM
#5
Frenzied Member
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.
-
Dec 2nd, 2005, 04:46 PM
#6
Addicted Member
Re: Arraylist doesn't sort right
oh sorry, i'm still a noob, I thought he was asking something I was helped with earlier.
-
Dec 2nd, 2005, 05:35 PM
#7
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
 
-
Dec 2nd, 2005, 06:03 PM
#8
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.
-
Dec 3rd, 2005, 06:26 AM
#9
Thread Starter
Fanatic Member
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.
-
Dec 3rd, 2005, 07:18 AM
#10
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.
-
Dec 3rd, 2005, 07:25 AM
#11
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.
-
Dec 3rd, 2005, 07:27 AM
#12
Thread Starter
Fanatic Member
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:
Public Class clsCard
Inherits Object
Implements IComparable
Public Overloads Function CompareTo(ByVal pCard As Object) As Integer Implements IComparable.CompareTo
If TypeOf pCard Is clsCard Then
Dim cCard As clsCard
cCard = pCard
If Me.Sign.CompareTo(cCard.Sign) = 0 Then
If Me.Points = 0 And cCard.Points = 0 Then
Return Me.Value.CompareTo(cCard.Value)
Else
Return Me.iCardPoint.CompareTo(cCard.Points)
End If
Else
Return Me.Sign.CompareTo(cCard.Sign)
End If
cCard = Nothing
Else
Throw New ArgumentException("Object is not a Card-object.")
End If
End Function
End Class
-
Dec 3rd, 2005, 09:23 AM
#13
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.
-
Dec 3rd, 2005, 09:33 AM
#14
Thread Starter
Fanatic Member
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)
-
Dec 3rd, 2005, 09:43 AM
#15
Re: Arraylist doesn't sort right
VB Code:
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:
Return Me.Points.CompareTo(cCard.Points)
Last edited by jmcilhinney; Dec 3rd, 2005 at 09:47 AM.
-
Dec 3rd, 2005, 09:46 AM
#16
Thread Starter
Fanatic Member
Re: Arraylist doesn't sort right
i had to replace
VB Code:
Return Me.iCardPoint.CompareTo(cCard.Points)
by
VB Code:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|