I have a person list and I want to sort it first by pos number and get last entry with the higher pos and then if there are more entries with the same pos number , sort them by date and get last date and show the results in a new list. The
problem is when I get more entries with the same pos number i get the entry with first date .
List<Person> people = new List<Person>();
Person p1 = new Person() { Name = "john", data = new DateTime(2015, 9, 4), pos = 0 };
Person p2 = new Person() { Name = "danny", data = new DateTime(2015, 6, 19), pos = 1 };
Person p3 = new Person() { Name = "john", data = new DateTime(2015, 4, 5), pos = 1 };
Person p4 = new Person() { Name = "danny", data = new DateTime(2015, 7, 19), pos = 1 };
Person p5 = new Person() { Name = "john", data = new DateTime(2015, 3, 5), pos = 2 };
people.Add(p1); people.Add(p2); people.Add(p3); people.Add(p4); people.Add(p5);
List<Person> ordered = people.OrderByDescending(x => x.pos).ThenBy(x => x.data).ToList();
List<Person> unique = new List<Person>();
foreach (Person result in ordered)
{
if (unique.Any(x => x.Name == result.Name))
continue;
unique.Add(result);
}
foreach (Person result in unique)
{
Console.WriteLine(result.Name + " @ " + result.data);
}