Generic List in C# provides easy way to sort list. In C# some times we need to add data to generic list and sort list by ascending based on our need. C# .net provides .sort method for List that can be used for sorting the contents of list. We will see example to sort list in c#.
Sample code to sort list [C#]:
using System;
using System.Collections.Generic;
public class sortlist
{
public static void Main()
{
List<string> names = new List<string>();
names.Add("John");
names.Add("Antony");
names.Add("Mickel");
names.Add("Daniel");
names.Sort();
Console.WriteLine();
foreach(string name in names)
{
Console.WriteLine(name);
}
}
}
//Output //Antony //Daniel //John //Mickel