Given the following data:
List<Country> countries = new List<Country>(); countries.Add(new Country{ Name = "USA", population=500000, Year=2012 }); countries.Add(new Country{ Name = "USA", population=300000, Year=2002 }); countries.Add(new Country{ Name = "USA", population=250000, Year=1992 }); countries.Add(new Country{ Name = "USA", population=20000, Year=1982 }); countries.Add(new Country{ Name = "India", population=1500000, Year=2012 }); countries.Add(new Country{ Name = "India", population=1000000, Year=2002 }); countries.Add(new Country{ Name = "India", population=50000, Year=1982 }); countries.Add(new Country{ Name = "India", population=80000, Year=1992 }); countries.Add(new Country{ Name = "Germany", population=100000, Year=2012 }); countries.Add(new Country{ Name = "Germany", population=400000, Year=2002 }); countries.Add(new Country{ Name = "Germany", population=60000, Year=1992 }); countries.Add(new Country{ Name = "Germany", population=4000, Year=1982 }); countries.Add(new Country{ Name = "UK", population=450000, Year=2002 }); countries.Add(new Country{ Name = "UK", population=50000, Year=1992 }); countries.Add(new Country{ Name = "UK", population=3000, Year=1982 });
I want to order the countries with the largest population for a given year, but then show all the years for that country before moving to the next country.
eg.
2012 - the order of the population - India, USA, UK, Germany. Therefore, I would like the data to be ordered for all data from India, all data from the United States, all British data, and then Germany.
2002 - the order of the population - India, USA, Germany, and then Great Britain. The UK is the last because it does not have data for 2002.
I want to achieve this using LINQ, although I have used LINQ in the past, I am struggling to think it through. Any help would be greatly appreciated.
source share