Many times I have come across sorting a list or searching an object from list.
Here I am putting those code blocks that can help us to do so....
Sorting
List
//Sort A-Z
persons.Sort(delegate(Person x, Person y)
{
return x.Name.CompareTo(y.Name);
});
For Z-A sorting, we should use,
Searching
List
Person p =
{
if (x.Name == "abc")
return true;
else
return false;
});
The above delegate implementation can also be replaced with static function call as shown below.
List
Person p = persons.Find(IsPersonInList);
private static bool IsPersonInList(Person x)
{
if(x.Name == "abc")
return true;
else
return false;
}
Yep ! That's It...
Happy Coding__
No comments:
Post a Comment