Sorting in List<> Generics in Csharp
// Create Resource class with variables(Members)
public class Resources
{
public string Name { get; set; }
public string url { get; set; }
public int id { get; set; }
public string Image { get; set; }
public string post { get; set; }
public DateTime Createddate { get; set; }
public string ListName { get; set; }
public static int Comparedate(Resources r1, Resources r2)
{
// To display in descending order multiply with -1
return r1.Createddate.CompareTo(r2.Createddate) * -1;
}
}
List<Resources> Rs = new List<Resources>();
Add values to the list as below.
Resources r2 = new Resources();
r2.post = PostItem["Title"].ToString();
r2.Name = "";
r2.url = "";
r2.Image = ImageUrl.ToString();
r2.id = PostItem.ID;
r2.Createddate = Convert.ToDateTime(PostItem["Created"]);
r2.ListName = "Posts";
Rs.Add(r2);
Resources r3 = new Resources();
if (postedLink["Notes"] != null)
r3.post = postedLink["Notes"].ToString();
r3.Name = postedLink["URL"].ToString().Substring(0, postedLink["URL"].ToString().IndexOf(','));
r3.url = postedLink["URL"].ToString().Substring(0, postedLink["URL"].ToString().IndexOf(','));
r3.Image = ImageUrl.ToString();
r3.id = postedLink.ID;
r3.Createddate = Convert.ToDateTime(postedLink["Created"]);
r3.ListName = "Links";
Rs.Add(r3);
Comparison<Resources> ComparedList = new Comparison<Resources>(Resources.Comparedate);
Rs.Sort(ComparedList);
Assign ‘Rs’list to the repeater or gridview.
Source: http://shafaqat309.wordpress.com/2009/05/04/sorting-generic-collections-list-in-cnet-net-2-0-net-3-5-c-classes-collections-generics-sorting/