- All columns are always#No column is not present in table. I just mentioned here to give detail about entries.
- 1#1,2,5 are special "clubbed" entries and should be sorted together on Entry which have number column set to 1. These entries have specialty that their rate and description would be same and Number would be 1 and 2.
- There is possibility that there could be some entries which have empty Entry values; such entries will have number set to 0. Such as 3#3
- There is possibility that there could be some entries which have single Entry value and will not have any pair and Number column for such entries would be set to 1 and there wouldn't be any entry in list which have same description and rate with Number column set to 2. Such as #4.
- While sorting clubbed entries on Entry column only consider Entry with Number set to 1 and other entry should tag along.
- All columns are always present. I just mentioned here to give detail about entries.
- 1,2,5 are special "clubbed" entries and should be sorted together on Entry which have number column set to 1. These entries have specialty that their rate and description would be same and Number would be 1 and 2.
- There is possibility that there could be some entries which have empty Entry values; such entries will have number set to 0. Such as 3
- There is possibility that there could be some entries which have single Entry value and will not have any pair and Number column for such entries would be set to 1 and there wouldn't be any entry in list which have same description and rate with Number column set to 2. Such as #4.
- While sorting clubbed entries on Entry column only consider Entry with Number set to 1 and other entry should tag along.
- #No column is not present in table. I just mentioned here to give detail about entries.
- #1,2,5 are special "clubbed" entries and should be sorted together on Entry which have number column set to 1. These entries have specialty that their rate and description would be same and Number would be 1 and 2.
- There is possibility that there could be some entries which have empty Entry values; such entries will have number set to 0. Such as #3
- There is possibility that there could be some entries which have single Entry value and will not have any pair and Number column for such entries would be set to 1 and there wouldn't be any entry in list which have same description and rate with Number column set to 2. Such as #4.
- While sorting clubbed entries on Entry column only consider Entry with Number set to 1 and other entry should tag along.
- 87.9k
- 14
- 104
- 325
Sorting Listtable using multiple condition c#columns
I want to sort entries in a given list on Entry column in most efficient way. BelowHere is the example how my i entries looks like.look:
#No Detail Entry Number Rate
1 Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
2 Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
3 Done something at 4$ "" 0 4
4 Done something else at 9$ M 1 9
5 Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
#No Detail Entry Number Rate
1 Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
2 Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
3 Done something at 4$ "" 0 4
4 Done something else at 9$ M 1 9
5 Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Some explanation about data: ###Some explanation of the format:
- No column is not available in listAll columns are always present. I just mentioned here to give detail about entries.
- 1,2,5 are special clubbed"clubbed" entries and should be sorted together on Entry which have number column set to 1. These entries have specialty that their rate and description would be same and Number would be 1 and 2.
- There is possibility that there could be some entries which have empty Entry values,values; such entries will have number set to 0. Such as 3
- There is possibility that there could be some entries which have single Entry value and will not have anany pair and Number column for such entries would be set to 1 and there wouldn't be any entry in list which have same description and rate with Number column set to 2. Such as #4.
- While sorting clubbed entries on Entry column only consider Entry with Number set to 1 and other entry should tag along.
My Solution: ###My Solution:
LINQ solution ###LINQ solution
Output: ###Output:
Detail Entry Number Rate
Done something at 4$ "" 0 4
Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
Done something else at 9$ M 1 9
Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
Detail Entry Number Rate
Done something at 4$ "" 0 4
Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
Done something else at 9$ M 1 9
Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
Question: ###Questions:
- I have Linq solution working, Any comments on that?
- Can it be done using IComparer
IComparer
as in rest of my code had sorting done using IComparerIComparer
?In Compare InCompare
method, I have switch case on column where I compare two entries but I don't know how to group while comparing.
Sorting List using multiple condition c#
I want to sort entries in a given list on Entry column in most efficient way. Below is the example how my i entries looks like.
#No Detail Entry Number Rate
1 Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
2 Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
3 Done something at 4$ "" 0 4
4 Done something else at 9$ M 1 9
5 Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Some explanation about data:
- No column is not available in list. I just mentioned here to give detail about entries.
- 1,2,5 are special clubbed entries and should be sorted together on Entry which have number column set to 1. These entries have specialty that their rate and description would be same and Number would be 1 and 2.
- There is possibility that there could be some entries which have empty Entry values, such entries will have number set to 0. Such as 3
- There is possibility that there could be some entries which have single Entry value and will not have an pair and Number column for such entries would be set to 1 and there wouldn't be any entry in list which have same description and rate with Number column set to 2. Such as #4.
- While sorting clubbed entries on Entry column only consider Entry with Number set to 1 and other entry should tag along.
My Solution:
LINQ solution
Output:
Detail Entry Number Rate
Done something at 4$ "" 0 4
Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
Done something else at 9$ M 1 9
Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
Question:
- I have Linq solution working, Any comments on that?
- Can it be done using IComparer as in rest of my code had sorting done using IComparer?In Compare method I have switch case on column where I compare two entries but I don't know how to group while comparing.
Sorting table using multiple columns
I want to sort entries in a given list on Entry column in most efficient way. Here is how my entries look:
#No Detail Entry Number Rate
1 Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
2 Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
3 Done something at 4$ "" 0 4
4 Done something else at 9$ M 1 9
5 Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
###Some explanation of the format:
- All columns are always present. I just mentioned here to give detail about entries.
- 1,2,5 are special "clubbed" entries and should be sorted together on Entry which have number column set to 1. These entries have specialty that their rate and description would be same and Number would be 1 and 2.
- There is possibility that there could be some entries which have empty Entry values; such entries will have number set to 0. Such as 3
- There is possibility that there could be some entries which have single Entry value and will not have any pair and Number column for such entries would be set to 1 and there wouldn't be any entry in list which have same description and rate with Number column set to 2. Such as #4.
- While sorting clubbed entries on Entry column only consider Entry with Number set to 1 and other entry should tag along.
###My Solution:
###LINQ solution
###Output:
Detail Entry Number Rate
Done something at 4$ "" 0 4
Watched movie at 6$ B 1 6
Watched movie at 6$ Z 2 6
Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
Done something else at 9$ M 1 9
Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
###Questions:
- I have Linq solution working, Any comments on that?
- Can it be done using
IComparer
as in rest of my code had sorting done usingIComparer
? InCompare
method, I have switch case on column where I compare two entries but I don't know how to group while comparing.
LINQ solution
var sortedList = entries.GroupBy(x => new { x.Detail, x.Rate }) // Grouping entries which have same Detail and Rate
.OrderBy(x => x.FirstOrDefault(y => y.Number <= 1)?.Entry) // Sort the group based on Entry which has Number set to <=1
.SelectMany(x => x.OrderBy(z => z.Number)) // Un-Group entries and make sure order is maintained using Number property
.ToList();
Output:
Output:
LINQ solution
var sortedList = entries.GroupBy(x => new { x.Detail, x.Rate }) // Grouping entries which have same Detail and Rate
.OrderBy(x => x.FirstOrDefault(y => y.Number <= 1)?.Entry) // Sort the group based on Entry which has Number set to <=1
.SelectMany(x => x.OrderBy(z => z.Number)) // Un-Group entries and make sure order is maintained using Number property
.ToList();
Output: