-1

As may people know, string.Compare compares texts with numbers as "xx10yy" < "xx2yy", however, windows does it as "xx10yy" > "xx2yy".

The question is how to compare texts as windows does, if there's a function that does it.

Thanks in advance.

Edit :

The question was written in a lot of ways in a lot of questions, mine has been more strict in my opinion and since the answer I've provided is written by me I prefer answering to a question of my like.

Edit 2 :

Just for it not to be marked as a duplicate, since it isn't, at least not of one that I'm aware of.

  • Windows Explorer does it the second way, you could implement a natural sort algorithm to do the same. – Kevin Jan 04 '19 at 19:32
  • @WelcomeOverflow the question is explained different there and mine is more strict in my opinion, i also provided an answer written by me so i prefer answering to a question of my like. – Matan Yashar Jan 04 '19 at 19:38
  • if the question is how to "compare texts as windows does" using the same code that Windows does is pretty much going to be the gold standard (and a dupe). You should know that there is a really good solution in java which is fairly easy to convert. – Ňɏssa Pøngjǣrdenlarp Jan 04 '19 at 19:43

1 Answers1

0

That's the way I've done it, I'm sure it'll help some, please tell me if any of you finds bugs in it.

public class HumanizeComparisonType
{
    public class GetHumanizeComparisonType
    {
        public bool CaseSensitiveCondition;// True - case sensitive, False - case insensitive
        public string
            FirstText
            , SecondText;
    }

    public static int HumanizeComparison(GetHumanizeComparisonType GetHumanizeComparison)// 0 - Equal, 1 - The first text is bigger, 2 - The second text is bigger
    {
        GetHumanizeComparisonType FunctionGet = GetHumanizeComparison;
        int
            FunctionResult = 0
            , FirstTextIndex = -1
            , FirstTextNumber
            , FirstTextLength = FunctionGet.FirstText.Length
            , SecondTextIndex = -1
            , SecondTextNumber
            , SecondTextLength = FunctionGet.SecondText.Length
            , SmallestTextLength = FirstTextLength > SecondTextLength ? SecondTextLength : FirstTextLength;

        bool MethodFound = false;

        for (int TextsIndex = 0; TextsIndex < SmallestTextLength; TextsIndex++)
            if
            (
                FunctionGet.FirstText[TextsIndex] >= '0' && FunctionGet.FirstText[TextsIndex] <= '9'
                && FunctionGet.SecondText[TextsIndex] >= '0' && FunctionGet.SecondText[TextsIndex] <= '9'
            )
            {
                switch (String.Compare(FunctionGet.FirstText, FirstTextIndex + 1, FunctionGet.SecondText, FirstTextIndex + 1, TextsIndex - (FirstTextIndex + 1), FunctionGet.CaseSensitiveCondition == false))
                {
                    case -1:
                        {
                            MethodFound = true;

                            FunctionResult = 2;

                            break;
                        }

                    case 0:
                        {
                            for (FirstTextIndex = TextsIndex; FirstTextIndex < FirstTextLength - 1; FirstTextIndex++)
                                if (FunctionGet.FirstText[FirstTextIndex + 1] < '0' || FunctionGet.FirstText[FirstTextIndex + 1] > '9')
                                    break;

                            for (SecondTextIndex = TextsIndex; SecondTextIndex < SecondTextLength - 1; SecondTextIndex++)
                                if (FunctionGet.SecondText[SecondTextIndex + 1] < '0' || FunctionGet.SecondText[SecondTextIndex + 1] > '9')
                                    break;

                            FirstTextNumber = int.Parse(FunctionGet.FirstText.Substring(TextsIndex, FirstTextIndex - TextsIndex + 1));

                            SecondTextNumber = int.Parse(FunctionGet.SecondText.Substring(TextsIndex, SecondTextIndex - TextsIndex + 1));

                            if (FirstTextNumber > SecondTextNumber)
                            {
                                MethodFound = true;

                                FunctionResult = 1;
                            }
                            else
                            {
                                if (SecondTextNumber > FirstTextNumber)
                                {
                                    MethodFound = true;

                                    FunctionResult = 2;
                                }
                                else
                                {
                                    if (FirstTextIndex > SecondTextIndex)// checking if there're leading zeroes before the number of the first text
                                    {
                                        MethodFound = true;

                                        FunctionResult = 2;
                                    }
                                    else
                                    {
                                        if (SecondTextIndex > FirstTextIndex)// checking if there're leading zeroes before the number of the second text
                                        {
                                            MethodFound = true;

                                            FunctionResult = 1;
                                        }
                                        else
                                            TextsIndex = FirstTextIndex;
                                    }
                                }
                            }

                            break;
                        }

                    case 1:
                        {
                            MethodFound = true;

                            FunctionResult = 1;

                            break;
                        }
                }

                if (MethodFound == true)
                    break;
            }

        if (MethodFound == false)
            if (FirstTextIndex < FirstTextLength - 1)
                if (SecondTextIndex < SecondTextLength - 1)
                    switch (String.Compare(FunctionGet.FirstText.Substring(FirstTextIndex + 1), FunctionGet.SecondText.Substring(FirstTextIndex + 1), FunctionGet.CaseSensitiveCondition == false))
                    {
                        case -1:
                            {
                                FunctionResult = 2;

                                break;
                            }

                        case 1:
                            {
                                FunctionResult = 1;

                                break;
                            }
                    }
                else
                    FunctionResult = 1;
            else
            {
                if (SecondTextIndex < SecondTextLength - 1)// else means that both of the texts are equal, FunctionResult = 0
                    FunctionResult = 2;
            }

        return FunctionResult;
    }
}