Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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
Posted
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()

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.

Posted

/** 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 !

"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

Posted

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:

  • Administrators
Posted (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 by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

"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

Posted

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

Posted
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.

Posted
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.
  • Administrators
Posted

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)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...