I have a table (as in a list of numbers) that gets recreated through a series of linq queries. I'm trying to sort the columns in descending order based upon the values within the first row.
My Database looks like this:
Data
{
ColumnLabel,
RowLabel,
Value
}
{"Cat", "All", 9}
{"Dog","All", 17}
{"Fish", "All", 4}
{"Cat", "Girls", 3}
{"Dog","Girls", 2}
{"Fish", "Girls", 2}
{"Cat", "Boys", 6}
{"Dog","Boys", 15}
{"Fish", "Boys", 2}
When I naively join this table together it looks like this:
Cat | Dog | Fish
All 9 17 4
Girls 3 15 2
Boys 6 2 2
What I want to do is sort the ColumnLabels
in descending order based on Value
where RowLabel == "All"
. The way I WANT the table to look is like this:
Dog | Cat | Fish
All 17 9 4
Girls 15 3 2
Boys 2 6 2
Notice the Dog and Cat row flipped.
I'll need to out put this table using HTML so I iterate through by each row.
I start by sorting the ColumnLabels
:
var top = data.Where(x => x.RowLabel == "All") .OrderBy(x => x.Data) .Select(x => x.ColumnLabel).SingleOrDefault();
Now top
contains the order I want the Columns to appear; {Dog, Cat, Fish}
. I don't really care how the Rows are sorted vertically.
What I then want to do is group each row based upon it's RowLabel
value:
var row = data.GroupBy(x => x.RowLabel)
.OrderBy(y => ??????????????);
//I want to OrderBy the values in top
//How do I do this?
I then iterate through each row and create my HTML table. This part is easy, but I don't know how to order subsequent rows based upon the top order. How do I do this in LINQ?