Skip to content

Instantly share code, notes, and snippets.

@skalinets
Created October 5, 2011 11:03
Show Gist options
  • Save skalinets/1264186 to your computer and use it in GitHub Desktop.
Save skalinets/1264186 to your computer and use it in GitHub Desktop.
data = PersonHelper.SortPersonsByAge(data, Constants.MinAge, Constants.MaxAge);
// ....
/// <summary>
/// Sorts list of persons
/// </summary>
public static List<Person> SortPersonsByAge(IList<Person> persons, int minAge, int maxAge)
{
List<Person> result = new List<Person>();
int iDelta = maxAge - minAge;
if (iDelta > 0)
{
SortedDictionary<int, List<Person>> sorted = new SortedDictionary<int, List<Person>>();
List<Person> tmp;
foreach (var person in persons)
{
if(sorted.ContainsKey(person.Age))
{
sorted[person.Age].Add(person);
}
else
{
tmp = new List<Person>();
tmp.Add(person);
sorted.Add(person.Age, tmp);
}
}
foreach (var personList in sorted)
{
result.AddRange(personList.Value);
}
}
else
{
result = (List<Person>)persons;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment