philmckendry
philmckendry

Reputation: 565

Grouping by an property in a list and the sorting these grouped elements by another property without affecting the original grouping

Hi I have a list of sports matches that I would like to group by competition, then sort by start time, without affecting the grouping of the competition in c# (aka group by competition and then sort for start date):

So essentially what I need is this:

Match            Start Time Competition

red vs blue        17:40    league 1
yellow vs green    15:00    league 2
brown vs orange    12:00    league 1
purple vs maroon   20:00    league 3
gold vs silver     16:45    league 1
white vs black     19:00    league 3
grey vs navy       18:00    league 1

converted into this:

Match         Start Time    Competition

brown vs orange    12:00    league 1
gold vs silver     16:45    league 1
red vs blue        17:40    league 1
grey vs navy       18:00    league 1
yellow vs green    15:00    league 2
white vs black     19:00    league 3
purple vs maroon   20:00    league 3

The code I have currently does not work and im kind of at a loss of where to go next. Any ideas would be much appreciated. Here is the code below:

  var matchListSorted =
                        list1.GroupBy(rows=> rows.Competition)
                            .SelectMany(row=> row.OrderBy(rows=> rows.ExpectedOffDate))
                            .ToList();

EDIT this code currently groups by competition but then disregards it in favour of sorting by start time as shown below

Match            Start Time   Competition
brown vs orange     12:00   league 1
yellow vs green     15:00   league 2
gold vs silver      16:45   league 1
red vs blue         17:40   league 1
grey vs navy        18:00   league 1
white vs black      19:00   league 3
purple vs maroon    20:00   league 3

Upvotes: 2

Views: 79

Answers (2)

philmckendry
philmckendry

Reputation: 565

The original code i had worked

var matchListSorted =
                    list1.GroupBy(rows=> rows.Competition)
                        .SelectMany(row=> row.OrderBy(rows=> rows.ExpectedOffDate))
                        .ToList();

I have no idea why it did not work the first few times. I attempted the same code a few hours later with added test data (including the original) and it worked.

Upvotes: 0

Nkosi
Nkosi

Reputation: 247323

Not sure if my answer would be considered off topic, but I was able to achieve the result with out grouping using just order by.

[TestClass]
public class LinqTests {

    [TestMethod]
    public void Linq_Sort() {
        var input = new[] 
        { 
        new {Match="red vs blue",StartTime="17:40",Competition="league 1"},
        new {Match="yellow vs green",StartTime="15:00",Competition="league 2"},
        new {Match="brown vs orange",StartTime="12:00",Competition="league 1"},
        new {Match="purple vs maroon",StartTime="20:00",Competition="league 3"},
        new {Match="gold vs silver",StartTime="16:45",Competition="league 1"},
        new {Match="white vs black",StartTime="19:00",Competition="league 3"},
        new {Match="grey vs navy",StartTime="18:00",Competition="league 1"},
        };

        var matchListSorted = (from match in input
                               orderby match.Competition, match.StartTime                                  
                               select match);


        var result = matchListSorted.ToList();
    }
}

And got the following result

+       [0] { Match = "brown vs orange", StartTime = "12:00", Competition = "league 1" }    <Anonymous Type>
+       [1] { Match = "gold vs silver", StartTime = "16:45", Competition = "league 1" } <Anonymous Type>
+       [2] { Match = "red vs blue", StartTime = "17:40", Competition = "league 1" }    <Anonymous Type>
+       [3] { Match = "grey vs navy", StartTime = "18:00", Competition = "league 1" }   <Anonymous Type>
+       [4] { Match = "yellow vs green", StartTime = "15:00", Competition = "league 2" }    <Anonymous Type>
+       [5] { Match = "white vs black", StartTime = "19:00", Competition = "league 3" } <Anonymous Type>
+       [6] { Match = "purple vs maroon", StartTime = "20:00", Competition = "league 3" }   <Anonymous Type>

Then tried it this way and got the same result

[TestMethod]
public void Linq_GroupSort() {
    var input = new[] 
    { 
    new {Match="red vs blue",StartTime="17:40",Competition="league 1"},
    new {Match="yellow vs green",StartTime="15:00",Competition="league 2"},
    new {Match="brown vs orange",StartTime="12:00",Competition="league 1"},
    new {Match="purple vs maroon",StartTime="20:00",Competition="league 3"},
    new {Match="gold vs silver",StartTime="16:45",Competition="league 1"},
    new {Match="white vs black",StartTime="19:00",Competition="league 3"},
    new {Match="grey vs navy",StartTime="18:00",Competition="league 1"},
    };

    var matchListSorted = (from match in input
                           orderby match.Competition
                           group match by match.Competition into g
                           select g).SelectMany(g => g.OrderBy(m => m.StartTime));


    var result = matchListSorted.ToList();
}

Upvotes: 1

Related Questions