We can use LINQ order by function to sort a list with some strings.
private void ListSort()
{
List<string> lst = new List<string>();
lst.Add("janaka");
lst.Add("lahiru");
lst.Add("chamara");
lst.Add("iroshan");
lst.Add("asanka");
foreach (string item in lst.OrderBy(r => r)) // Sort by ascending order
{
Response.Write(item.ToString() + "<br/>");
}
Response.Write("<br/>");
foreach (string item in lst.OrderByDescending(r => r)) // Sort by descending order
{
Response.Write(item.ToString() + "<br/>");
}
}
Output:
asanka
chamara
iroshan
janaka
lahiru
lahiru
janaka
iroshan
chamara
asanka
Sunday, December 30, 2012
How to Sort List in C#.NET
Subscribe to:
Post Comments (Atom)
No comments:
Write comments