Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Group and then Sort list of tuple <T1,T2,T3>

I'm new to C# and I want to sort my collection of List<Tuple<int, string, int>>

The user inputs data for example:

T1   T2   T3
1  abbc   3             
1  becky  5           
1  betty  56           
2  Olivia 6             
2  abbc   3            
2  becky  5           
3  Olivia 675

I would like the sort list to look like;

 ID = 1 { abbc,3|becky,5|betty,56 }        
 ID = 2 { Olivia,6|abbc,3|becky,5 }          
 ID = 3 { Olivia,675 }

please help i'm really stuck :)

like image 866
helpme Avatar asked Dec 24 '22 02:12

helpme


1 Answers

It looks more like you want to Group your items and then Order by the first int (Id):

List<Tuple<int, string, int>> data = new List<Tuple<int, string, int>>
{
    new Tuple<int, string, int>(1,"abbc",3),
    new Tuple<int, string, int>(1,"becky",5),
    new Tuple<int, string, int>(1,"betty",56),
    new Tuple<int, string, int>(2,"Olivia",6),
    new Tuple<int, string, int>(2,"abbc",3),
    new Tuple<int, string, int>(2,"becky",5),
    new Tuple<int, string, int>(3,"Olivia",675),
};

var result = data.GroupBy(item => item.Item1)
                 .Select(group => new { Id = group.Key, Values = group.Select(item => new { item.Item2, item.Item3 }).ToList() })
                 .OrderBy(item => item.Id).ToList();
like image 198
Gilad Green Avatar answered Dec 30 '22 15:12

Gilad Green