Sorted collection
-
I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }
-
I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }
-
I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }
There are two ways to go: - you could use a collection that sorts itself everytime you add/remove an item; those are pretty good if the sorting criterium is constant. - you could use any collection that fits your app, then sort it if and when you need it sorted; you can specify the sort criterium dynamically. This article[^] deals with the subject. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I am working on an application where I have created a class emp which has ID and Name as a property. Now I want to create a collection of Emp class , which should be sorted based on the either ID or Name. (Program will decide whether it want to data sorted based on the Name or Id at the runtime). For and employ ID and NAMe property is not unique. It is possible that employed is having similar name. Can you please help me to decide which .net collection object should be used to achieve above functionality? If possible please provide a snippet. public class Emp { private int myID; public int ID { get { return myID; } set { myID = value; } } private string myName; public string Name { get { return myName; } set { myName = value; } } }
Possible solution.
public class Emp
{
private int myID;
public int ID
{
get { return myID; }
set { myID = value; }
}private string myName;
public string Name
{
get { return myName; }
set { myName = value; }
}public static List<Emp> GetEmployees()
{
//return a list of emp objects
}
}In calling code use:
//Using var allows type returned by GetEmployees to be
//any thing that implements IEnumerable<T>
var emps = Emp.GetEmployees(); //Fill the list
foreach (Emp emp in emps.OrderBy(e => e.Name))
{
Console.WriteLine(emp);
}foreach (Emp emp in emps.OrderBy(e => e.ID))
{
Console.WriteLine(emp);
}Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.
-
There are two ways to go: - you could use a collection that sorts itself everytime you add/remove an item; those are pretty good if the sorting criterium is constant. - you could use any collection that fits your app, then sort it if and when you need it sorted; you can specify the sort criterium dynamically. This article[^] deals with the subject. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I guess you meant: -you could use a collection that sorts itself everytime you add/remove an item; :)
yes, that would be good. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).