Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Sortable List<t></t>

Sortable List<t></t>

Scheduled Pinned Locked Moved C#
designbusinesshelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggsovereasy
    wrote on last edited by
    #1

    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;
    	}
    }
    

    }

    L 1 Reply Last reply
    0
    • E eggsovereasy

      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;
      	}
      }
      

      }

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups