S
Sudip_inn
Guest
I have unordered list of quarters stored in List<string> and i want to sort that data by LINQ. After Sorting order should look like below list of quarters.
2010 FY 2011 FY 2012 FY
1Q 2013 2Q 2013 1H 2013 3Q 2013 4Q 2013 2H 2013 2013 FY
1Q 2014 2Q 2014 1H 2014 3Q 2014 4Q 2014 2H 2014 2014 FY
i got one bit similar post from here How Order by Case in LINQ
which i try to follow but now facing compilation error.
This way i tried
var sortOrder = new Dictionary<string,int>
{
{ "1Q", 1 },
{ "2Q", 2 },
{ "1H", 3 },
{ "2Q", 4 },
{ "3Q", 5 },
{ "2H", 6 },
{ "4Q", 7 },
{ "FY", 8 }
};
var defaultOrder = sortOrder.Max(x => x.Value) + 1;
List<string> _lstQtr = new List<string>();
_lstQtr.Add("2H 2013");
_lstQtr.Add("1Q 2013");
_lstQtr.Add("2Q 2013");
_lstQtr.Add("2013 FY");
_lstQtr.Add("1H 2013");
_lstQtr.Add("3Q 2013");
_lstQtr.Add("4Q 2013");
_lstQtr.Add("2H 2014");
_lstQtr.Add("2Q 2014");
_lstQtr.Add("2014 FY");
_lstQtr.Add("1H 2014");
_lstQtr.Add("3Q 2014");
_lstQtr.Add("4Q 2014");
_lstQtr.Add("1Q 2014");
_lstQtr.Add("2012 FY");
_lstQtr.Add("2010 FY");
_lstQtr.Add("2011 FY");
_lstQtr = _lstQtr.OrderBy(s => (s.Contains("FY") ? s.Substring(0, 4) : s.Substring(s.Length - 4)))
.ThenBy(p => (sortOrder.TryGetValue(p, out var order) ? order : defaultOrder))
.ToList();
What is wrong in this line for which getting error
.ThenBy(p => (sortOrder.TryGetValue(p, out var order) ? order : defaultOrder))
Please suggest me a good approach to order my quarters as a i mention how quarters should appear at top.
if order by year and then order by quarters. 1Q means 1, 2Q means 2, 1H means 3....this way
first three quarter has no 1Q or 2Q only has FY they should be sorted by years and rest of the quarters has 1Q or 2Q they also should be sorted first by year and then sort again by 1Q and 2Q postion.
please tell me how could i do this without using regex. thanks
Continue reading...
2010 FY 2011 FY 2012 FY
1Q 2013 2Q 2013 1H 2013 3Q 2013 4Q 2013 2H 2013 2013 FY
1Q 2014 2Q 2014 1H 2014 3Q 2014 4Q 2014 2H 2014 2014 FY
i got one bit similar post from here How Order by Case in LINQ
which i try to follow but now facing compilation error.
This way i tried
var sortOrder = new Dictionary<string,int>
{
{ "1Q", 1 },
{ "2Q", 2 },
{ "1H", 3 },
{ "2Q", 4 },
{ "3Q", 5 },
{ "2H", 6 },
{ "4Q", 7 },
{ "FY", 8 }
};
var defaultOrder = sortOrder.Max(x => x.Value) + 1;
List<string> _lstQtr = new List<string>();
_lstQtr.Add("2H 2013");
_lstQtr.Add("1Q 2013");
_lstQtr.Add("2Q 2013");
_lstQtr.Add("2013 FY");
_lstQtr.Add("1H 2013");
_lstQtr.Add("3Q 2013");
_lstQtr.Add("4Q 2013");
_lstQtr.Add("2H 2014");
_lstQtr.Add("2Q 2014");
_lstQtr.Add("2014 FY");
_lstQtr.Add("1H 2014");
_lstQtr.Add("3Q 2014");
_lstQtr.Add("4Q 2014");
_lstQtr.Add("1Q 2014");
_lstQtr.Add("2012 FY");
_lstQtr.Add("2010 FY");
_lstQtr.Add("2011 FY");
_lstQtr = _lstQtr.OrderBy(s => (s.Contains("FY") ? s.Substring(0, 4) : s.Substring(s.Length - 4)))
.ThenBy(p => (sortOrder.TryGetValue(p, out var order) ? order : defaultOrder))
.ToList();
What is wrong in this line for which getting error
.ThenBy(p => (sortOrder.TryGetValue(p, out var order) ? order : defaultOrder))
Please suggest me a good approach to order my quarters as a i mention how quarters should appear at top.
if order by year and then order by quarters. 1Q means 1, 2Q means 2, 1H means 3....this way
first three quarter has no 1Q or 2Q only has FY they should be sorted by years and rest of the quarters has 1Q or 2Q they also should be sorted first by year and then sort again by 1Q and 2Q postion.
please tell me how could i do this without using regex. thanks
Continue reading...