473,394 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

using icomparable

hi,
I have a class called orders, which has a DateTime dateCreated
property, telling me when each order was created. I get the orders
returned to me in a collection, as an array. but now i need to sort
them by datecreated. i've tried the following:
// implement IComparable
public class ProjOrderHistory : IComparable
{
public DateTime dateCreated;
....
public int CompareTo(object obj)
{
OrderHistorySummary temp = (OrderHistorySummary)obj;
return
this.dateCreated.CompareTo(temp.dateCreated);
}
}

and when i build it i get back an ProjOrderHistory [] object called
_orders. Now _orders is populating fine with the order information,
but when i try to call
Array.Sort(_orders)
i get "Expression has been evaluated and has no value"
am i missing something, or is my CompareTo wrong?

Thanks.

Jun 22 '07 #1
2 1565
Sorry the compareTo should be like this:
public int CompareTo(object obj)
{
ProjOrderHistory temp = (ProjOrderHistory)obj;
return
this.dateCreated.CompareTo(temp.dateCreated);
}

Jun 22 '07 #2
Where do you get that message? As an exception? Or when you call
Array.Sort in the immediate window?

Have you looked to see if you data is now sorted? Array.Sort does an
in-place sort on the array you pass it - and it looks like it should
be fine. Another point; if DateCreated isn't the *natural* sequence
for this object, then personally I would tend to *not* implement
IComparable, and use an external IComparer / IComparer<T/
Comparison<T- e.g. (of the latter):

Array.Sort(_orders, delegate(ProjOrderHistory lhs,
ProjOrderHistory rhs) {
return DateTime.Compare(lhs.DateCreated, rhs.DateCreated);
});

Jun 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cybertof | last post by:
Hello, Could someone please explain me the difference between the 2 interfaces IComparable and IComparer ? In which cases use one or the other ? Are they dedicated to own classes or built-in...
1
by: raffelm | last post by:
In the first class below, I get an error that AnotherClientBaseDataRecord does not implement IComparable.CompareTo(object)...yet the second class below is implemented identicially and it does get...
2
by: ESPNSTI | last post by:
Hi, I'm trying to use a generics dictionary with a key class that implements and needs IComparable<>. However when I attempt to use the dictionary, it doesn't appear to use the IComparable<> to...
4
by: CodeRazor | last post by:
Hi, I want to retrieve a sorted list of files, ordered by LastWriteTime. I know I can implement IComparer, but I don't know how or what this means. I know a sortedlist has objects and keys and...
2
by: Stefan L | last post by:
Hi everybody, when migrating to the .NET 2.0 framework we decided to encourage the use of generic functions because they ought to run faster than their equivalent object-implemantations. Now...
15
by: Gustaf | last post by:
Using VS 2005. I got an 'IpForm' class and an 'IpFormCollection' class, containing IpForm objects. To iterate through IpFrom objects with foreach, the class is implemented as such: public class...
3
by: Samuel R. Neff | last post by:
You can you program against generic interfaces generically? For example, how can I make the following code which works for the non-generic interface also work for the generic counterparts? ...
5
by: John Devlon | last post by:
Hi everyone, With this post I would like to ask you all a small question. I'm trying to sort an array of objects using IComparable. Adding below displayed code to a class and triggering the...
1
by: Tony Johansson | last post by:
Hello! Assume I have this IComparable interface and I implement this IComparable in my class called MyTop see below. If I now inherit a new class called MySub from MyTop this new class is not...
6
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. Now If I want to implement functionallity so I can compare animal with each other or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.