[solved] Proper way to sort a List<> Custom objects
-
I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.
struct MYSTRUCT { public double address; public short datLen; public string data; }
I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 10:14 PM
-
I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.
struct MYSTRUCT { public double address; public short datLen; public string data; }
I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 10:14 PM
For lots of examples of the various ways to do this, google c# sorting generic lists.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.
struct MYSTRUCT { public double address; public short datLen; public string data; }
I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 10:14 PM
If you always want to sort on one field (i.e. address) then implement IComparable<T> It's really simple:
struct MYSTRUCT : IComparable<MYSTRUCT>
{
public double address;
public short datLen;
public string data;public int CompareTo(MYSTRUCT other) { return address.CompareTo(other.address); }
}
Now just call Sort on your collection and it will automatically use your CompareTo method.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.
struct MYSTRUCT { public double address; public short datLen; public string data; }
I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 10:14 PM
Hi, I think I covered that from several angles in this little article[^]. :)
Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig:
-
I have been thinking about this for a while and want to be correct in how I go about this. I have a custom struct.
struct MYSTRUCT { public double address; public short datLen; public string data; }
I am using an array for these, and chose the List<> because of the Add method. What I want to do is resort the list based on the Address highest to lowest. I am pretty unfamiliar with the IComparer and Comparison option in the built in List<>Sort. Will one of those work for me? Do I override the Sort method? Do I just create a new method to sort the list? How would you guys go about this? Thanks in advance.
***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW
modified on Sunday, August 30, 2009 10:14 PM
Are you sure address is a double? for home addresses, I'd expect a string. for enumerable addresses (as in memory addresses), I would want an integer (probably uint/int or ulong/long) which is exact and maybe can be used as an array index, not something that needs conversions from double to int all the time. :)
Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig:
-
Are you sure address is a double? for home addresses, I'd expect a string. for enumerable addresses (as in memory addresses), I would want an integer (probably uint/int or ulong/long) which is exact and maybe can be used as an array index, not something that needs conversions from double to int all the time. :)
Luc Pattyn
:badger: :jig: :badger:
Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.
:jig: :badger: :jig: