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 :)
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With