p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

csharptoday_discuss thread: want to discuss sorting arrays of objects


Message #1 by "Chris Hart" <cahart@c...> on Mon, 10 Dec 2001 21:42:24 -0600
If I have an array of objects, such as the array of Entry objects on page

816 of Chapter-24 in the "Beginning C#" book.

And I want to explicitly sort that array by calling the sort method of the

array class (ie., Array.Sort(entries) )

why do I get the following error at run-time:



"At least one object must implement IComparable."



An Entry obj has the following properties:

		private String _filename;

		private DateTime _timestamp;

		private String _title;

		private String _details;



I really don't care if I sort on _filename or _timestamp.



Would very much appreciate clarification on this.



Thanks in advance

Chris



Message #2 by "Frank Paris" <marshalt@s...> on 10 Dec 2001 21:23:17 -0800
The Sort method must know how to compare two elements of your array. The

IComparable interface defines the CompareTo() method, which you must

implement in order to make the comparison. In your CompareTo() method

you would compare this._filename or this._timestamp to the _filename or

_timestamp in the Entry object input to your IComparable.CompareTo()

method and return a value telling the Sort method whether 'this' is

greater, equal to, or less than the input Entry.



Frank Paris

marshalt@s...





> -----Original Message-----

> From: Chris Hart [mailto:cahart@c...] 

> Sent: Monday, December 10, 2001 7:42 PM

> To: C#Today Discuss

> Subject: [csharptoday_discuss] want to discuss sorting arrays 

> of objects

> 

> 

> If I have an array of objects, such as the array of Entry 

> objects on page 816 of Chapter-24 in the "Beginning C#" book. 

> And I want to explicitly sort that array by calling the sort 

> method of the array class (ie., Array.Sort(entries) ) why do 

> I get the following error at run-time:

> 

> "At least one object must implement IComparable."

> 

> An Entry obj has the following properties:

> 		private String _filename;

> 		private DateTime _timestamp;

> 		private String _title;

> 		private String _details;

> 

> I really don't care if I sort on _filename or _timestamp.

> 

> Would very much appreciate clarification on this.

> 

> Thanks in advance

> Chris



Message #3 by "Chris Hart" <cahart@c...> on Tue, 11 Dec 2001 08:24:16 -0600
Hi Frank,

Thanks very much for taking the time to respond to my question.



After I submitted it I found an example in my "A Programmer's Intro. To C#"

book (pg. 298 for anyone interested) that showed exactly what you are

explaining.

I implemented a CompareTo method that compares the _timestamp property and I

now get the results I want.



Regards,

Chris



-----Original Message-----

From: Frank Paris [mailto:marshalt@s...]

Sent: Monday, December 10, 2001 11:23 PM

To: C#Today Discuss

Subject: [csharptoday_discuss] RE: want to discuss sorting arrays of

objects





The Sort method must know how to compare two elements of your array. The

IComparable interface defines the CompareTo() method, which you must

implement in order to make the comparison. In your CompareTo() method

you would compare this._filename or this._timestamp to the _filename or

_timestamp in the Entry object input to your IComparable.CompareTo()

method and return a value telling the Sort method whether 'this' is

greater, equal to, or less than the input Entry.



Frank Paris

marshalt@s...





> -----Original Message-----

> From: Chris Hart [mailto:cahart@c...]

> Sent: Monday, December 10, 2001 7:42 PM

> To: C#Today Discuss

> Subject: [csharptoday_discuss] want to discuss sorting arrays

> of objects

>

>

> If I have an array of objects, such as the array of Entry

> objects on page 816 of Chapter-24 in the "Beginning C#" book.

> And I want to explicitly sort that array by calling the sort

> method of the array class (ie., Array.Sort(entries) ) why do

> I get the following error at run-time:

>

> "At least one object must implement IComparable."

>

> An Entry obj has the following properties:

> 		private String _filename;

> 		private DateTime _timestamp;

> 		private String _title;

> 		private String _details;

>

> I really don't care if I sort on _filename or _timestamp.

>

> Would very much appreciate clarification on this.

>

> Thanks in advance

> Chris







cahart@c...


$subst('Email.Unsub')




  Return to Index