none
How to arrange my data in specific way 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 ? provide some sample code. thanks

    Saturday, April 6, 2019 7:46 PM

Answers

  • Check an example:

    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( );
    


    • Edited by Viorel_MVP Saturday, April 6, 2019 8:48 PM
    • Proposed as answer by Fouad Roumieh Sunday, April 7, 2019 4:28 AM
    • Marked as answer by Sudip_inn Monday, April 8, 2019 7:33 PM
    Saturday, April 6, 2019 8:46 PM
  • FYA$ and FY$ why $ is used ?
    what is the meaning of these ^.Q why exponent symbol used ?
    what is the meaning of 4 \d\d\d\d ?

    All of these are special codes that are interpreted by Regex.Match.

    The "$" means "Anchor to the right", that is, the text that is tested for a match needs to end with the letters before the $.

    The "^" means "Anchor to the left", that is, the text that is tested for a match needs to start with the letters after the ^.

    The "\d" means "any digit (0 to 9)". So the four \d mean "we need to match four digits here".


    Sunday, April 7, 2019 4:16 PM
    Moderator
  • Ok, here is an example. Imagine that you have a string such as "abcdefgh" and you want to test it against a regular expression such as "gh$", like this:

    if (Regex.IsMatch("abcdefgh", "gh$") ...

    then IsMatch will return true because the string ends in gh.

    Now let's do the same with ef$:

    if (Regex.IsMatch("abcdefgh", "ef$") ...

    now the anwser is false because the string does not end in ef.

    So what happens if we drop the "$"?

    if (Regex.IsMatch("abcdefgh", "ef") ...

    now the answer is true because the string does contain "ef" and we are not forcing the ef to be at the end.

    • Marked as answer by Sudip_inn Monday, April 8, 2019 7:32 PM
    Sunday, April 7, 2019 8:41 PM
    Moderator
  • It's perfectly symmetrical with the other. If you understood the one for "$" then the one for "^" should be completely obvious. Change "end" into "start" and everything should fit immediately.
    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:37 PM
    Monday, April 8, 2019 8:48 PM
    Moderator
  • It changes the order of the Sort. The "ThenBy" is a clause for the "OrdeBy" that tells it the next value to use for sorting when the preceding ones are equal. The result of the Regex is a boolean. When you order by a boolean, it sorts first the "false" values and then the "true". By adding the !, you reverse the order of the sorting.

    I believe that you would obtain the same result if you used .ThenByDescending instead of .ThenBy with a !.

    • Marked as answer by Sudip_inn Sunday, October 27, 2019 8:24 AM
    Friday, October 25, 2019 6:34 PM
    Moderator

All replies

  • There is no easy way, this data doesn't follow any numerical order or alphabetical. You will need to do it one by one and build a sorted list out of the loop.

    Are those records from the Database? 


    Fouad Roumieh

    Saturday, April 6, 2019 7:55 PM
  • Check an example:

    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( );
    


    • Edited by Viorel_MVP Saturday, April 6, 2019 8:48 PM
    • Proposed as answer by Fouad Roumieh Sunday, April 7, 2019 4:28 AM
    • Marked as answer by Sudip_inn Monday, April 8, 2019 7:33 PM
    Saturday, April 6, 2019 8:46 PM
  • please tell me the meaning of these 4 lines

    .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 )
    FYA$ and FY$ why $ is used ?

    what is the meaning of these ^.Q why exponent symbol used ?

    what is the meaning of 4 \d\d\d\d ?

    please guide further. thanks a lot for answer.

    Sunday, April 7, 2019 12:01 PM
  • FYA$ and FY$ why $ is used ?
    what is the meaning of these ^.Q why exponent symbol used ?
    what is the meaning of 4 \d\d\d\d ?

    All of these are special codes that are interpreted by Regex.Match.

    The "$" means "Anchor to the right", that is, the text that is tested for a match needs to end with the letters before the $.

    The "^" means "Anchor to the left", that is, the text that is tested for a match needs to start with the letters after the ^.

    The "\d" means "any digit (0 to 9)". So the four \d mean "we need to match four digits here".


    Sunday, April 7, 2019 4:16 PM
    Moderator
  • these two explanation still not clear. can u explain these two with a easy example. thanks

    The "$" means "Anchor to the right", that is, the text that is tested for a match needs

    to end with the letters before the $. The "^" means "Anchor to the left", that is, the text that is tested for a match needs

    to start with the letters after the ^.



    • Edited by Sudip_inn Sunday, April 7, 2019 6:24 PM
    Sunday, April 7, 2019 6:24 PM
  • Ok, here is an example. Imagine that you have a string such as "abcdefgh" and you want to test it against a regular expression such as "gh$", like this:

    if (Regex.IsMatch("abcdefgh", "gh$") ...

    then IsMatch will return true because the string ends in gh.

    Now let's do the same with ef$:

    if (Regex.IsMatch("abcdefgh", "ef$") ...

    now the anwser is false because the string does not end in ef.

    So what happens if we drop the "$"?

    if (Regex.IsMatch("abcdefgh", "ef") ...

    now the answer is true because the string does contain "ef" and we are not forcing the ef to be at the end.

    • Marked as answer by Sudip_inn Monday, April 8, 2019 7:32 PM
    Sunday, April 7, 2019 8:41 PM
    Moderator
  • what about this explanation....you miss this one.

    The "^" means "Anchor to the left", that is, the text that is tested for a match needs

    to start with the letters after the ^.

    Monday, April 8, 2019 7:33 PM
  • It's perfectly symmetrical with the other. If you understood the one for "$" then the one for "^" should be completely obvious. Change "end" into "start" and everything should fit immediately.
    • Marked as answer by Sudip_inn Tuesday, April 9, 2019 7:37 PM
    Monday, April 8, 2019 8:48 PM
    Moderator
  • Thanks lot
    Wednesday, September 25, 2019 7:26 PM
  • @Viorel 

    what is the meaning of using ! symbol in regex code

     .ThenBy( s => !Regex.Match( s, @"FYA$" ).Success )
    .ThenBy( s => !Regex.Match( s, @"FY$" ).Success )

    what does mean !Regex.Match here used ! symbol....why it is used ?


    Friday, October 25, 2019 12:42 PM
  • what is the meaning of using ! symbol in regex code

    No, the ! is not in the Regex code. It is outside of the Regex.

    So, first the Regex is executed, and returns true or false. Then the ! is applied to this result. In C#, the ! means "NOT". So it converts the true into false and the false into true.

    Friday, October 25, 2019 2:45 PM
    Moderator
  • i asked why they did it ? 
    .ThenBy( s => !Regex.Match( s, @"FYA$" ).Success )
    why they use not operator outside of regex? what was the objective of using not operator there? explain if you understand please.
    Friday, October 25, 2019 3:35 PM
  • It changes the order of the Sort. The "ThenBy" is a clause for the "OrdeBy" that tells it the next value to use for sorting when the preceding ones are equal. The result of the Regex is a boolean. When you order by a boolean, it sorts first the "false" values and then the "true". By adding the !, you reverse the order of the sorting.

    I believe that you would obtain the same result if you used .ThenByDescending instead of .ThenBy with a !.

    • Marked as answer by Sudip_inn Sunday, October 27, 2019 8:24 AM
    Friday, October 25, 2019 6:34 PM
    Moderator
  • you said It changes the order of the Sort. The "ThenBy" is a clause for the "OrdeBy" that tells it the next value to use for sorting when the preceding ones are equal.

    i remove ! sign from regex then also get the same result....why?

    Regex.Match( s, @"FYA$" ).Success

    one more question is that why adding 5 ?

    .ThenBy(s => Regex.Match(s, @"^.Q").Value + '5')
    please explain



    Wednesday, October 30, 2019 3:03 PM
  • i remove ! sign from regex then also get the same result....why?

    If there are no repetitions in the field used by the OrderBy, then the ThenBy doesn't make any difference. The order will be forced by the first field.

    If the first field is repeated, then the ThenBy is applied, but if all the rows in a repeated group have the same value in the ThenBy (all are true or all are false in this case) then it will not make any difference, and the next ThenBy (if there is one) will be applied.

    why adding 5 ?

    No idea. Maybe the person who provided that example can answer this last question.

    Wednesday, October 30, 2019 4:57 PM
    Moderator
  • @Viorel

    you answer the above question.

    i want to know

    why adding 5 ?

    .ThenBy(s => Regex.Match(s, @"^.Q").Value + '5')

    .ThenBy( s => !Regex.Match( s, @"FY$" ).Success )

    why not sign ! has been added there when without ! sign code works fine. tell me reason.

    Tuesday, November 5, 2019 8:02 AM