Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4616262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:57:24+00:00 2026-05-22T01:57:24+00:00

I’m having a List<String> l_lstTemp and which contains A1 A1_1 A1_2 1A B2_1 B1_2

  • 0

I’m having a List<String> l_lstTemp and which contains

"A1"
"A1_1"
"A1_2"
"1A"
"B2_1"
"B1_2"
"B1_1_2"
"A10"
"B11"
"A"
"Z"

I need to sort the items based on the character and numeric value.

So the sorted list will be like

"1A"
"A"
"A1"
"A1_1"
"A1_2"
"A10"
"B1_1_2"
"B1_2"
"B2_1"
"B11"
"Z"

Here is my code:

l_lstTemp.Sort(delegate(String One, String Two)
{
    Match l_mOne = Regex.Match(One, @"(\D*)(\d*)");
    Match l_mTwo = Regex.Match(Two, @"(\D*)(\d*)");
    int Result;
    if (l_mOne.Success || l_mTwo.Success)
    {
        String l_strX, l_strY;
        l_strX = l_mOne.Groups[1].Value;
        l_strY = l_mTwo.Groups[1].Value;
        Result = l_strX.CompareTo(l_strY);
        if (Result != 0)
            return Result;
        l_strX = l_mOne.Groups[2].Value;
        l_strY = l_mTwo.Groups[2].Value;
        if (l_strX == String.Empty || l_strY == String.Empty)
        {
            Result = l_strX.CompareTo(l_strY);
            if (Result != 0)
                return Result;
        }
        else
        {
            long X = long.Parse(l_strX);
            long Y = long.Parse(l_strY);
            Result = X.CompareTo(Y);
            if (Result != 0)
                return Result;
        }

    }
    return 0 ;
}
);

But its not working (sorting) properly.

How do I modify my code to sort the list properly?

Please post me a way to do this.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:57:25+00:00Added an answer on May 22, 2026 at 1:57 am

    I did some modifications to your code. The thing was that when both Group 1 and Group 2 are equals, you still need to check what remains.

    Important: I did the modifications inside your code, so this could be a little tricky. I really suggest you refactoring your code now that you know it works:

    l.Sort(delegate(String One, String Two)
    {
        while (One != "" && Two != "")
        {
            if (One == Two)
                return 0;
    
            //Add one more group to capture what remains of the expression
            Match l_mOne = Regex.Match(One, @"_*(\D*)(\d*)(.*)$"); 
            Match l_mTwo = Regex.Match(Two, @"_*(\D*)(\d*)(.*)$");
            int Result;
            if (l_mOne.Success || l_mTwo.Success)
            {
                String l_strX, l_strY;
                l_strX = l_mOne.Groups[1].Value;
                l_strY = l_mTwo.Groups[1].Value;
                Result = l_strX.CompareTo(l_strY);
                if (Result != 0)
                    return Result;
                l_strX = l_mOne.Groups[2].Value;
                l_strY = l_mTwo.Groups[2].Value;
                if (l_strX == String.Empty || l_strY == String.Empty)
                {
                    Result = l_strX.CompareTo(l_strY);
                    if (Result != 0)
                        return Result;
                }
                else
                {
                    long X = long.Parse(l_strX);
                    long Y = long.Parse(l_strY);
                    Result = X.CompareTo(Y);
                    if (Result != 0)
                        return Result;
                    One = l_mOne.Groups[3].Value; //Store in 'One' the remaining part of the regex
                    Two = l_mTwo.Groups[3].Value; //The same in Two
    
                    continue; //The result will be the result of the comparison of those two new values.
                }
    
            }
        }
        return One.CompareTo(Two);
    });
    

    Edit:
    I also added _* to remove all the _ characters from the begining of the strings. I assumed here that the strings will only contain _ after the numbers and not something like B1B or B1$.

    The thing here is that you don’t really explain how the comparison should be made, and I had to assume those things from your original data and the sorted data, otherwise what would happen if you want to sort A1A and A1_? What should it return?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.