CrookDawg Posted May 12, 2004 Posted May 12, 2004 I am creating a program that stores and sorts lists of dvds. This program uses a structure that has Title, Additional Info, and purchase date. I need to know how to sort this dynamic array using the "Title" variable. I have searched everywhere and can find no help. I am new at vb.net and appreciate any help. Thanks Quote
Joe Mamma Posted May 12, 2004 Posted May 12, 2004 I am creating a program that stores and sorts lists of dvds. This program uses a structure that has Title' date=' Additional Info, and purchase date. I need to know how to sort this dynamic array using the "Title" variable. I have searched everywhere and can find no help. I am new at vb.net and appreciate any help. Thanks[/quote'] have your cd class implement IComparable and define a CompareTo method for the class class CDClass: IComparable { string m_title; object m_additionalInfo; datetime m_purchaseDate; public int CompareTo(object obj) { if(obj is CDClass) { CDClass aCd = (CDClass) obj; return m_title.CompareTo(aCd.m_title); } throw new ArgumentException("object is not a CD"); } } Store Your objects in an ArrayList, cdList; Call cdList.Sort() Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Arch4ngel Posted May 12, 2004 Posted May 12, 2004 /** ADDITIONAL INFORMATION **/ You can even make a struct that implement the IComparable interface. I tried it an it compile (doesn't tested it). However... class is way more customizable. :) Have fun ! Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
CrookDawg Posted May 12, 2004 Author Posted May 12, 2004 Thanks everyone. im still confused on the ICompare and i think i best leave it alone for now (im a beginner). I found a way to do it using two loops (do and for). here it is, it might be a bit crude, but it works :-\ Public Shared Sub Sort(ByRef DVDs() As DVDRecord) Dim i As Integer Dim Done As Boolean = False Dim tmpDVD As DVDRecord Do While Not Done Done = True For i = 0 To UBound(DVDs) - 1 If DVDs(i).Title > DVDs(i + 1).Title Then tmpDVD = DVDs(i) DVDs(i) = DVDs(i + 1) DVDs(i + 1) = tmpDVD Done = False End If Next Loop End Sub [/Code] Thanks for all your help! :cool: Quote
Administrators PlausiblyDamp Posted May 12, 2004 Administrators Posted May 12, 2004 (edited) You would definately be better of looking more into Joe Mamma's suggestion - using IComparable will result in you having to write much less code. If you prefer a VB version Public Class CDClass 'Public Structure CDClass - would work just as well, doesn't have to be a class Implements IComparable Private _Title As String Private _PurchaseDate As Date Private _AdditionalInfo As Object Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo If TypeOf obj Is CDClass Then Dim c As CDClass c = DirectCast(obj, CDClass) Return _Title.CompareTo(c._Title) Else Throw New ArgumentException("Can only compare to CDClass") End If End Function End Class If you have an array of these called CDs the following code will work Dim CDs() as cdclass 'Set array up how you would normally 'redim the array and add to it etc..... Array.Sort(CDs) 'CDs is now sorted Edited May 12, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Arch4ngel Posted May 12, 2004 Posted May 12, 2004 I agree with PlausiblyDamp. Implement this. It would give you access to more features like Array.Sort() and Array.IndexOf() <== not sure about this one Implementing .NET interface give you access to pre-made functions that already exists. Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
CrookDawg Posted May 12, 2004 Author Posted May 12, 2004 Thanks all. I'll try to implement this. I don't have much time becuase of school and other stuff, so i'll try to get this in as soon as possible. Quote
CrookDawg Posted May 12, 2004 Author Posted May 12, 2004 I GOT IT!!! :p Thanks every one. i used the IComparable. Thank you for all your help. It helped alot. :D :D :D Quote
CrookDawg Posted May 12, 2004 Author Posted May 12, 2004 Now i have just one more question. I used the code that PlausiblyDamp supplied (modified just a bit). How would i go about giving the user the option to sort by author, or location in a cd/dvd changer along with the sort by name option? Thanks Quote
HJB417 Posted May 13, 2004 Posted May 13, 2004 Now i have just one more question. I used the code that PlausiblyDamp supplied (modified just a bit). How would i go about giving the user the option to sort by author, or location in a cd/dvd changer along with the sort by name option? Thanks You'll need to use an object that implements IComparer. If you're using an object of Type Array, you can use the static Array.Sort method. As for implementing a comparer, you could do something like (your code is vb, mine is psuedo c#, sorry) Also, becareful when u say/use 'structure' in .net as classes, structures are different. [public, internal, private, ...] class CDClassAuthorComparer : IComparer { int IComparer.Compare(object x, object y) { CDClass lhs = x as CDClass; CDClass rhs = y as CDClass; if((lhs == null) || (rhs == null)) throw new(ArgumentException("x and/or y are not of typeCDClass.")); return lhs.Author.CompareTo(rhs.Author); } } I wrote this in notepad so check for errors. Quote
CrookDawg Posted May 13, 2004 Author Posted May 13, 2004 I really appreciate this, but i could really use some code or ideas in vb as i only know this language right now. Still learning. im not sure of what an object that implements IComparer exactly is. Thanks again. Quote
Administrators PlausiblyDamp Posted May 13, 2004 Administrators Posted May 13, 2004 The following should give you an idea - proper error handling / naming conventions etc really should be considered though ;) Public Class CDClass 'Public Structure CDClass - would work just as well, doesn't have to be a class Public Class SortPurchaseDate Implements IComparer Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare Dim cd1, cd2 As CDClass cd1 = DirectCast(x, CDClass) cd2 = DirectCast(y, CDClass) Return cd1._PurchaseDate.CompareTo(cd2._PurchaseDate) End Function End Class Public Class SortTitle Implements IComparer Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare Dim cd1, cd2 As CDClass cd1 = DirectCast(x, CDClass) cd2 = DirectCast(y, CDClass) Return cd1._Title.CompareTo(cd2._Title) End Function End Class Public _Title As String Public _PurchaseDate As Date Public _AdditionalInfo As Object Public Shared Function TitleComparer() As IComparer Return DirectCast(New SortTitle, IComparer) End Function Public Shared Function PurchaseComparer() As IComparer Return DirectCast(New SortPurchaseDate, IComparer) End Function End Class to use them code like the following should work Dim cds(3) As CDClass cds(0) = New CDClass cds(0)._Title = "apple" cds(0)._PurchaseDate = Date.Now cds(1) = New CDClass cds(1)._Title = "orange" cds(1)._PurchaseDate = Date.Now.Subtract(TimeSpan.FromHours(3)) cds(2) = New CDClass cds(2)._Title = "banana" cds(2)._PurchaseDate = Date.Now.Subtract(TimeSpan.FromHours(1)) cds(3) = New CDClass cds(3)._Title = "Airplane" cds(3)._PurchaseDate = Date.Now.AddHours(3) Array.Sort(cds, CDClass.PurchaseComparer) Array.Sort(cds, CDClass.TitleComparer) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
CrookDawg Posted May 13, 2004 Author Posted May 13, 2004 Thanks, im going to try this now. i think i am getting the hang of the icomparer thing. i have a simple question though, what exactly does "directcast" do? I've seen it alot and i was just wondering. Thanks Quote
Administrators PlausiblyDamp Posted May 14, 2004 Administrators Posted May 14, 2004 http://www.xtremedotnettalk.com/showthread.php?s=&threadid=73095 http://www.xtremedotnettalk.com/showthread.php?s=&threadid=70566 http://www.xtremedotnettalk.com/showthread.php?s=&threadid=80277 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.