none
How to arrange my Quater data ascending way without using Regex RRS feed

  • Question

  • i have data stored in List<String> and data look like a which stored in list

    1Q 2014A
    
    2Q 2014A
    
    2012FY
    
    1Q 2013A
    
    2Q 2013A
    
    4Q 2014A
    
    4Q 2013A
    
    2013 FYA
    
    2011FY
    
    3Q 2014A
    
    3Q 2013A
    
    2010FY
    
    2014 FYA

    i need to order or arrange data in list like below way. the order is small year comes first

    and then order quarter like 1Q 2010A, 2Q 2010A, 3Q 2010A,4Q 2010A, 2010 FYA. this way i need to order whole data.

    sample order data as follows

    2010FY
    
    2011FY
    
    2012FY
    
    1Q 2013A
    
    2Q 2013A
    
    3Q 2013A
    
    4Q 2013A
    
    2013 FYA
    
    1Q 2014A
    
    2Q 2014A
    
    3Q 2014A
    
    4Q 2014A
    
    2014 FYA
    tell me with minimum code how could i order data like above without regex? provide some sample code.

    i got to order with regex like below code but i need to do it without regex

    var list = new List<String>
        {
            "1Q 2014A",
            "2Q 2014A",
            "2012FY",
            "1Q 2013A",
            "2Q 2013A",
            "4Q 2014A",
            "4Q 2013A",
            "2013 FYA",
            "2011FY",
            "3Q 2014A",
            "3Q 2013A",
            "2013FY",
            "2010FY",
            "2014 FYA",
        };
    
    var result =
        list
            .OrderBy( s => Regex.Match( s, @"\d\d\d\d" ).Value )
            .ThenBy( s => Regex.Match( s, @"^.Q" ).Value + '5' )
            .ThenBy( s => !Regex.Match( s, @"FYA$" ).Success )
            .ThenBy( s => !Regex.Match( s, @"FY$" ).Success )
            .ToList( );

    thanks

    Wednesday, October 9, 2019 3:23 PM

Answers

  • Hi Sudip_inn,

    I hope the following code can help you.

            static void Main(string[] args)
            {
                List<string> lst = new List<string>() {
                    "1Q 2014A","2Q 2014A","2012FY","1Q 2013A","2Q 2013A","4Q 2014A","4Q 2013A",
                    "2013 FYA","2011FY","3Q 2014A","3Q 2013A","2010FY","2014 FYA"
                };
                List<string> sort1 = new List<string>();
                List<string> sort2 = new List<string>();
    
                for (int i =0;i<lst.Count;i++)
                {
                    if (lst[i].Contains("FY"))
                    {
                        sort1.Add(lst[i]);
                    }
                    else
                    {
                        sort2.Add(lst[i]);
                    }          
                }
                lst.Clear();
    
                var resultSort2 = sort2.OrderBy(s => Convert.ToInt32(s.Substring(3, 4)))
                                  .ThenBy(s => Convert.ToInt32(s.Substring(0, 1))).ToList();
                var resultSort1 = sort1.OrderBy(s => Convert.ToInt32(s.Substring(0, 4))).ToList();
               
                for (int i = 0; i < resultSort1.Count; i++)
                {
                    if (resultSort2[0].Substring(3, 4).Equals(resultSort1[i].Substring(0, 4)) || resultSort2[4].Substring(3, 4).Equals(resultSort1[i].Substring(0, 4)))
                    {
                        foreach (string str in resultSort2)
                        {
                            if (str.Substring(3, 4).Equals(resultSort1[i].Substring(0, 4)))
                            {
                                lst.Add(str);
                            }
                        }
                        lst.Add(resultSort1[i]);
                    }
                    else
                    {
                        lst.Add(resultSort1[i]);
                    }               
                }
                foreach (string str in lst)
                {
                    Console.WriteLine(str);
                }
                Console.ReadLine();
            }

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Thursday, October 10, 2019 9:02 AM
    Thursday, October 10, 2019 7:56 AM
    Moderator