473,385 Members | 1,347 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,385 software developers and data experts.

Sort Criteria for Generic.List ???

How does not specify the sort criteria for Generic.List ??
The way that this is done in C++ STL is to implement operator<(), how is this
done in C# and DotNet for Generic.List ???
Dec 23 '06 #1
3 2247
Check
6/5/2006
Custom Objects and Tiered Development II // 2.0

http://sholliday.spaces.live.com/blog/

And find the
public class CustomerComparer : IComparer<BusinessObjects.Customer>
{
}

class.

"Peter Olcott" <No****@SeeScreen.comwrote in message
news:AT*******************@newsfe12.phx...
How does not specify the sort criteria for Generic.List ??
The way that this is done in C++ STL is to implement operator<(), how is
this
done in C# and DotNet for Generic.List ???


Dec 23 '06 #2
Hi Peter,

You can sort a generic list easily using an anonymous method.

Take the following class for example:

class MyObject
{
// public for the sake of example
public string stringField;

public MyObject(string stringField)
{
this.stringField = stringField;
}
}

You can create a generic list of MyObjects as follows:

List<MyObjectlist = new List<MyObject>();

// note: add the objects unsorted on stringField:
list.Add(new MyObject("string 2"));
list.Add(new MyObject("string 3"));
list.Add(new MyObject("string 1"));

Sort them by their stringField value:

list.Sort(
// anonymous method
delegate(MyObject mo1, MyObject mo2)
{
// Perform any comparison that you'd like here.
// In this example, we're just comparing stringFields
return mo1.stringField.CompareTo(mo2.stringField);
});

Result:

Debug.Assert(list[0].stringField == "string 1", "index 0 invalid");
Debug.Assert(list[1].stringField == "string 2", "index 1 invalid");
Debug.Assert(list[2].stringField == "string 3", "index 2 invalid");

--
Dave Sexton

"Peter Olcott" <No****@SeeScreen.comwrote in message
news:AT*******************@newsfe12.phx...
How does not specify the sort criteria for Generic.List ??
The way that this is done in C++ STL is to implement operator<(), how is
this done in C# and DotNet for Generic.List ???

Dec 23 '06 #3
http://www.eggheadcafe.com/articles/...perty_sort.asp

--
Robbe Morris - 2004-2006 Microsoft MVP C#
I've mapped the database to .NET class properties and methods to
implement an multi-layered object oriented environment for your
data access layer. Thus, you should rarely ever have to type the words
SqlCommand, SqlDataAdapter, or SqlConnection again.
http://www.eggheadcafe.com/articles/..._generator.asp

"Peter Olcott" <No****@SeeScreen.comwrote in message
news:AT*******************@newsfe12.phx...
How does not specify the sort criteria for Generic.List ??
The way that this is done in C++ STL is to implement operator<(), how is
this done in C# and DotNet for Generic.List ???
Dec 23 '06 #4

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

Similar topics

1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
3
by: Math Preregistration System | last post by:
I'm using a std::list as a container for some pointers to objects, for example list< C* > lst; I would like to sort them using two different criteria, say first by C.first and then by...
4
by: Robin Tucker | last post by:
How do I sort the items in a list box? I am using a class derived from IComparer to sort items on columns in a ListView, but the ListBox doesn't support this kind of facility. The "items" in my...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
6
by: Nick Valeontis | last post by:
I know how to use Icomparable. However, I can't figure out how to sort a generic linked list? (without writing the algorithm) Lets say I have something like this: class...
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
9
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have a generic list that is populated with a structure. I would like to be able to sort on values in the structure. I have public struct mergoutstruct { public Int32 projectnumber; public...
5
by: neocortex | last post by:
Hello! I am a newbie in Python. Recently, I get stuck with the problem of sorting by two criteria. In brief, I have a two-dimensional list (for a table or a matrix). Now, I need to sort by two...
3
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.