如何按字母排序字符串数组?

时间:2022-11-24 15:58:26

i've got a array of many strings. How can I sort the strings by alphabet?

我有很多字符串。如何按字母顺序对字符串进行排序?

3 个解决方案

#1


19  

Sounds like you just want to use the Array.Sort method.

听起来你只想使用Array.Sort方法。

Array.Sort(myArray)

There are many overloads, some which take custom comparers (classes or delegates), but the default one should do the sorting alphabetically (ascending) as you seem to want.

有许多重载,其中一些采用自定义比较器(类或委托),但默认情况下应按字母顺序(升序)进行排序,如您所愿。

#2


2  

class Program

    {
        static void Main()
        {
            string[] a = new string[]
            {
                "Egyptian",
                "Indian",
                "American",
                "Chinese",
                "Filipino",
            };
            Array.Sort(a);
            foreach (string s in a)
            {
                Console.WriteLine(s);
            }
        }
    }

#3


1  

Array.Sort also provides a Predicate-Overload. You can specify your sorting-behaviour there:

Array.Sort还提供Predicate-Overload。您可以在那里指定您的排序行为:

Array.Sort(myArray, (p, q) => p[0].CompareTo(q[0]));

You can also use LINQ to Sort your array:

您还可以使用LINQ对数组进行排序:

string[] myArray = ...;
string[] sorted = myArray.OrderBy(o => o).ToArray();

LINQ also empoweres you to sort a 2D-Array:

LINQ还授权您对2D数组进行排序:

string[,] myArray = ...;
string[,] sorted = myArray.OrderBy(o => o[ROWINDEX]).ThenBy(t => t[ROWINDEX]).ToArray();

The default sorting-behaviour of LINQ is also alphabetically. You can reverse this by using OrderByDescending() / ThenByDescending() instead.

LINQ的默认排序行为也按字母顺序排列。您可以使用OrderByDescending()/ ThenByDescending()来反转此操作。

#1


19  

Sounds like you just want to use the Array.Sort method.

听起来你只想使用Array.Sort方法。

Array.Sort(myArray)

There are many overloads, some which take custom comparers (classes or delegates), but the default one should do the sorting alphabetically (ascending) as you seem to want.

有许多重载,其中一些采用自定义比较器(类或委托),但默认情况下应按字母顺序(升序)进行排序,如您所愿。

#2


2  

class Program

    {
        static void Main()
        {
            string[] a = new string[]
            {
                "Egyptian",
                "Indian",
                "American",
                "Chinese",
                "Filipino",
            };
            Array.Sort(a);
            foreach (string s in a)
            {
                Console.WriteLine(s);
            }
        }
    }

#3


1  

Array.Sort also provides a Predicate-Overload. You can specify your sorting-behaviour there:

Array.Sort还提供Predicate-Overload。您可以在那里指定您的排序行为:

Array.Sort(myArray, (p, q) => p[0].CompareTo(q[0]));

You can also use LINQ to Sort your array:

您还可以使用LINQ对数组进行排序:

string[] myArray = ...;
string[] sorted = myArray.OrderBy(o => o).ToArray();

LINQ also empoweres you to sort a 2D-Array:

LINQ还授权您对2D数组进行排序:

string[,] myArray = ...;
string[,] sorted = myArray.OrderBy(o => o[ROWINDEX]).ThenBy(t => t[ROWINDEX]).ToArray();

The default sorting-behaviour of LINQ is also alphabetically. You can reverse this by using OrderByDescending() / ThenByDescending() instead.

LINQ的默认排序行为也按字母顺序排列。您可以使用OrderByDescending()/ ThenByDescending()来反转此操作。