using System.Collections.Generic;
public static void Main()
Console.WriteLine("List of unsorted");
Console.WriteLine("==================");
var listOfPenalty = new List<Penalty>
new Penalty("P1", 1, 1, 8, 1),
new Penalty("P2", 2, 2, 8, 2),
new Penalty("P3", 3, 3, 8, 1),
new Penalty("P4", 1, 4, 8, 2),
new Penalty("P5", 2, 5, 8, 1),
new Penalty("P6", 4, 4, 8, 1),
new Penalty("P7", 4, 4, 8, 2),
new Penalty("P8", 5, 4, 8, 2),
new Penalty("P9", 5, 8, 8, 2),
new Penalty("P10", 6, 10, 8, 1),
new Penalty("P11", 6, 10, 8, 1),
new Penalty("P12", 7, 10, 7, 1)
listOfPenalty = listOfPenalty.OrderBy(x => x.StartDate).ToList();
Console.WriteLine("PenaltyId | MatchId | StartDate | Type");
Console.WriteLine("-------------------------------------------");
foreach (var penalty in listOfPenalty)
Console.WriteLine(" " + (penalty.PenaltyId.Length == 2 ? " " : "") + penalty.PenaltyId + "\t|\t" + penalty.MatchId + "\t|\t" + penalty.StartDate.ToString("MMM dd") + "\t|\t" + penalty.Type);
Console.WriteLine("-------------------------------------------");
var sortedList = listOfPenalty.GroupBy(x => x.MatchId)
.SelectMany(grp => grp.Select(x => new
IsDifferentTypePresent = (grp.Count() > 1 && grp.Select(y => y.Type).Distinct().Count() > 1),
FirstStartDateOfGroup = grp.OrderBy(y => y.StartDate).First().StartDate
.OrderBy(x => x.FirstStartDateOfGroup)
.ThenByDescending(x => x.IsDifferentTypePresent ? x.Type : 0).ToList();
Console.WriteLine("List of sorted");
Console.WriteLine("==================");
Console.WriteLine("PenaltyId | MatchId | StartDate | Type");
Console.WriteLine("-------------------------------------------");
foreach (var penalty in sortedList)
Console.WriteLine(" " + (penalty.PenaltyId.Length == 2 ? " " : "") + penalty.PenaltyId + "\t|\t" + penalty.MatchId + "\t|\t" + penalty.StartDate.ToString("MMM dd") + "\t|\t" + penalty.Type);
Console.WriteLine("-------------------------------------------");
public Penalty(string penaltyId, int matchId, int datePart, int monthPart, int type)
StartDate = new DateTime(2021, monthPart, datePart);
public string PenaltyId { get; set; }
public int MatchId { get; set; }
public DateTime StartDate { get; set; }
public int Type { get; set; }