Sortable List<t></t>
-
I've being using objectDataSources to populate some data controls lately. My data access objects return collections of objects rather than DataTables or DataSets. The inherent problem with this is you can't sort a collection by any column (e.g. if I had an EmployeeCollection filled with Employee objects I couldn't sort by Employee.LastName in one method and then Employee.Id in another). So I set out to create a sortable list I could inherit from and be able to sort my collections. What I have is below, but the problem I have is that I'm converting my generic variables to strings so that I can compare them. I suppose this works fine as long as I override ToString() on my objects so that it makes sense, but it seems like a bit of a kludge. Does anyone have any other ideas that might work?
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;namespace IssueTracker.Business.Collections
{
public class SortableList<T> : List<T>
{
public void SortBy(string sortExpression, SortDirection sortDirection)
{
Sort(delegate(T t1, T t2) { return Compare(t1, t2, sortExpression, sortDirection); });
}private static int Compare(T t1, T t2, string propertyName, SortDirection sortDirection) { // get property values values object t1Value = t1.GetType().GetProperty(propertyName).GetValue(t1, null); object t2Value = t2.GetType().GetProperty(propertyName).GetValue(t2, null); // compare values StringComparer comparer = StringComparer.CurrentCulture; int result = comparer.Compare(t1Value.ToString(), t2Value.ToString()); // reverse result if it's descending if (sortDirection == SortDirection.Descending) result = result\*-1; return result; } }
}
-
I've being using objectDataSources to populate some data controls lately. My data access objects return collections of objects rather than DataTables or DataSets. The inherent problem with this is you can't sort a collection by any column (e.g. if I had an EmployeeCollection filled with Employee objects I couldn't sort by Employee.LastName in one method and then Employee.Id in another). So I set out to create a sortable list I could inherit from and be able to sort my collections. What I have is below, but the problem I have is that I'm converting my generic variables to strings so that I can compare them. I suppose this works fine as long as I override ToString() on my objects so that it makes sense, but it seems like a bit of a kludge. Does anyone have any other ideas that might work?
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;namespace IssueTracker.Business.Collections
{
public class SortableList<T> : List<T>
{
public void SortBy(string sortExpression, SortDirection sortDirection)
{
Sort(delegate(T t1, T t2) { return Compare(t1, t2, sortExpression, sortDirection); });
}private static int Compare(T t1, T t2, string propertyName, SortDirection sortDirection) { // get property values values object t1Value = t1.GetType().GetProperty(propertyName).GetValue(t1, null); object t2Value = t2.GetType().GetProperty(propertyName).GetValue(t2, null); // compare values StringComparer comparer = StringComparer.CurrentCulture; int result = comparer.Compare(t1Value.ToString(), t2Value.ToString()); // reverse result if it's descending if (sortDirection == SortDirection.Descending) result = result\*-1; return result; } }
}
I'm not sure what you are trying to do but List<> is already sortable
public class Employee
{
public string FName;
public string LName;
public Employee(string fname, string lname) { FName = fname; LName = lname; }public static int CompareFirstName(Employee e1, Employee e2)
{ return e1.FName.CompareTo(e2.FName); }
public static int CompareLastName(Employee e1, Employee e2)
{ return e1.LName.CompareTo(e2.LName); }
}List<Employee> list = new List<Employee>();
list.Add(new Employee("Joe", "Smakatelly"));
list.Add(new Employee("Sue", "Rummy"));list.Sort(Employee.CompareFirstName);
Console.WriteLine("First Name");
foreach (Employee e in list)
Console.WriteLine("{0} {1}", e.FName, e.LName);
list.Sort(Employee.CompareLastName);
foreach (Employee e in list)
Console.WriteLine("{0} {1}", e.FName, e.LName);led mike