Created
October 5, 2011 11:03
-
-
Save skalinets/1264186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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