Patrick Lorio
Patrick Lorio

Reputation: 5668

C# order string

How can I determine which string is before another if ordered alphabetically?

Examples:

is "add" before "ask"? true
is "aaaabba" before "aaac"? true
is "xeon" before "alphabet"? false

Is there something in .NET that can do this? Or does someone know of a good method to do this? Thanks in advance. I am using C#, but another language is fine for examples, or even pseudocode, or an idea, thanks.

Upvotes: 2

Views: 9069

Answers (7)

Niranjan Singh
Niranjan Singh

Reputation: 18290

If you are using some container then you can check index value to check this is it index before or not like this:

if( Array.IndexOf(strArray, "C") < Array.IndexOf(strArray, "C") ) return true;

As you know it is Ordered according to alphabet then you can use String.Compare Method (String, String)

retun  string.Compare(string1, string2) < 0 ? true : false

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters. For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

When comparing strings, you should call the Compare method, which requires that you explicitly specify the type of string comparison that the method uses. For more information, see Best Practices for Using Strings in the .NET Framework.

Upvotes: 0

Alex
Alex

Reputation: 8116

Cation at comparing culture-aware strings !

  String s1 = "Strasse";
  String s2 = "Straße";

  var res = String.Compare(s1, s2, StringComparison.Ordinal) == 0; // returns false

  CultureInfo ci = new CultureInfo("de-DE");
  eq = String.Compare(s1, s2, true, ci) == 0; // returns true

Upvotes: 4

Kevin Gosse
Kevin Gosse

Reputation: 39007

string.Compare is the function you're looking for.

If the result is greater than 0, then the first argument is greater than the second one. If it's lower than 0 then it's the second one. If the function returns 0, then both arguments are equal.

Upvotes: 0

MADMap
MADMap

Reputation: 3192

You can just use

int result = String.Compare(stringa, stringb);

If result > 0, then stringa would be before stringb, < 0 stringb before stringa and 0 for when they are the same.

Upvotes: 0

Steve B
Steve B

Reputation: 37660

Use string.Compare method

    static void Main(string[] args)
    {

        Console.WriteLine(
            "is 'add' before 'ask'? {0}", 
            string.Compare("add", "ask") < 0
            );
        Console.WriteLine(
            "is 'aaaabba' before 'aaac'? {0}",
            string.Compare("aaaabba", "aaac") < 0
            );
        Console.WriteLine(
            "is 'xeon' before 'alphabet'? {0}", 
            string.Compare("xeon", "alphabet") < 0
            );

        Console.ReadLine();
    }

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

You could use the Compare static method or CompareTo instance method:

class Program
{
    static void Main()
    {
        Console.WriteLine(string.Compare("add", "ask") < 0);
        Console.WriteLine(string.Compare("aaaabba", "aaac") < 0);
        Console.WriteLine(string.Compare("xeon", "alphabet") < 0);
    }
}

Prints:

True
True
False

Upvotes: 19

Matt Evans
Matt Evans

Reputation: 7575

Use the StringComparer class http://msdn.microsoft.com/en-us/library/system.stringcomparer.aspx

Upvotes: 0

Related Questions