How can I sort a List by order of case e.g.
- smtp:[email protected]
- smtp:[email protected]
- SMTP:[email protected]
I would like to sort so that the upper case record is first in the list e.g SMTP:[email protected].
How can I sort a List by order of case e.g.
I would like to sort so that the upper case record is first in the list e.g SMTP:[email protected].
I was writing another example while t4rzsan has answered =) I prefer t4rzsan´s answer... anyway, this is the answer I was writing.
//Like ob says, you could create your custom string comparer
public class MyStringComparer : IComparer<string>
{
public int Compare(string x, string y)
{
// Return -1 if string x should be before string y
// Return 1 if string x should be after string y
// Return 0 if string x is the same string as y
}
}
Example of using your own string comparer:
public class Program
{
static void Main(string[] args)
{
List<string> MyList = new List<string>();
MyList.Add("smtp:[email protected]");
MyList.Add("smtp:[email protected]");
MyList.Add("SMTP:[email protected]");
MyList.Sort(new MyStringComparer());
foreach (string s in MyList)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}