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.
#NoDetail Entry Number Rate
1Carpool at 5$ C 1 5
Carpool at 5$ H 2 5
2Played Cricket at 2$ X 1 2
Played Cricket at 2$ O 2 2
3Done something at 4$ "" 0 4
4Done something else at 9$ M 1 9
5Watched 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.
I want to sort Entry column by ascending or descending order abiding above rules.
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
My Solution:
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
internal class Data
{
public string Detail { get; set; }
public string Entry { get; set; }
public int Number { get; set; }
public int Rate { get; set; }
}
internal class Program
{
private static void Main(string[] args)
{
List<Data> entries = new List<Data>();
// Clubbed entry...While sorting only consider entry with Number set to 1. They will have same rate and Detail.
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "C", Number = 1, Rate = 5 });
entries.Add(new Data() { Detail = "Carpool at 5$", Entry = "H", Number = 2, Rate = 5 });
// Clubbed entry
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "X", Number = 1, Rate = 2 });
entries.Add(new Data() { Detail = "Played Cricket at 2$", Entry = "O", Number = 2, Rate = 2 });
// entry which have empty Entry value such entries will have Number set to 0
entries.Add(new Data() { Detail = "Done something at 4$", Entry = "", Number = 0, Rate = 4 });
// entry which 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 detail and rate with Number coloumn set to 2
entries.Add(new Data() { Detail = "Done something else at 9$", Entry = "M", Number = 1, Rate = 9 });
// Clubbed entry
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "B", Number = 1, Rate = 6 });
entries.Add(new Data() { Detail = "Watched movie at 6$", Entry = "Z", Number = 2, Rate = 6 });
// Sorting on Entry Coloumn
var sortedList = entries.GroupBy(x => x.Detail).OrderBy(x => x.FirstOrDefault(y => y.Number <= 1).Entry).SelectMany(x => x).ToList();
}
}
}
My Questions:
- My solution do not consider Rate and only groups based on Detail, can it be modified?
- Can it be done using IComparer as rest of my code had sorting done using IComparer?