See my code
i am trying to order Quarter. order would look like 1Q 2010 2Q 2010 1H 2010 3Q 2010 4Q 2010 2H 2010 2010 FY
List<string> lstperiods = new List<string>();
lstperiods.Add("1H 2015");
lstperiods.Add("1H 2014");
lstperiods.Add("1Q 2015");
lstperiods.Add("2Q 2015");
lstperiods.Add("3Q 2015");
lstperiods.Add("4Q 2015");
lstperiods.Add("2015 FY");
lstperiods.Add("1Q 2014");
lstperiods.Add("2Q 2014");
lstperiods.Add("3Q 2014");
lstperiods.Add("4Q 2014");
lstperiods.Add("2014 FY");
lstperiods.Add("2010 FY");
lstperiods.Add("2011 FY");
lstperiods.Add("2012 FY");
lstperiods.Add("2013 FY");
lstperiods.Add("2H 2015");
lstperiods.Add("2H 2014");
List<string> allperiods = new List<string>();
allperiods = lstperiods.OrderBy(x => (x.ToString().Contains("FY")
? x.ToString().Substring(0, 4)
: x.ToString().Substring(3, 4)))
.ThenBy(y => y switch
{
y.Contains("1Q") => 1,
y.Contains("2Q") => 2,
y.Contains("1H") => 3,
y.Contains("3Q") => 4,
y.Contains("4Q") => 5,
y.Contains("2H") => 6,
y.Contains("FY") => 7,
_ => 8
}).ToList();
the above code did not compile but when i change the code below way then it worked
List<string> allperiods = new List<string>();
allperiods = lstperiods.OrderBy(x => (x.ToString().Contains("FY")
? x.ToString().Substring(0, 4)
: x.ToString().Substring(3, 4)))
.ThenBy(y => y switch
{
string a when y.Contains("1Q") => 1,
string b when y.Contains("2Q") => 2,
string c when y.Contains("1H") => 3,
string d when y.Contains("3Q") => 4,
string e when y.Contains("4Q") => 5,
string f when y.Contains("2H") => 6,
string g when y.Contains("FY") => 7,
_ => 8
}).ToList();
1) tell me what is the meaning of this line string a when y.Contains("1Q") => 1,?
why do i need to declare multiple variable called a,b,c.....g?
2) also please show me other possible ways to compose the above same code ?
3) i have checked switch expression has multiple variation.
Thanks