对字符串数组进行排序

Jam*_*mes 0 c# asp.net sorting string

我正在从 Web 服务调用发送如下的 ArrayList:

私有ArrayList testList = new ArrayList();

它将存储如下值:

"xyz (pound) (4545)"
"abc (in)    (346)"
"def (off)   (42424)"
Run Code Online (Sandbox Code Playgroud)

我使用它有两个原因:

1:我必须在 ASP.NET 1.1 框架中获取这个值。

2:我使用 testList.Sort(); 在发送之前。

但现在我想将这些值发送为:

"xyz"   "pound"  "4545"
"abc"  "in"    "346"
"def"   "off" "42424"
Run Code Online (Sandbox Code Playgroud)

所以我找到了一种方法如下:

string[][] data = { new string[]{"xyz", "pound", "4545"},
                    new string[]{"abc", "in", "346"}, 
                    new string[]{"def", "off", "42424"}};
Run Code Online (Sandbox Code Playgroud)

问题是:我怎样才能有效地对其进行排序??或者有没有更好的方法来解决这个问题?

排序将基于第一个元素完成:

abc
def
xyz
Run Code Online (Sandbox Code Playgroud)

Hei*_*nzi 5

你写到你必须在 ASP 1.1 中读取这个值,所以我假设你在发送端有一个更现代的 .NET 框架版本。

如果是这种情况,您可以使用LINQ的OrderBy方法,包括在 Framework 3.5 或更高版本中:

string[][] data = { new string[] { "xyz", "pound", "4545" }, 
                    new string[] { "abc", "in", "346" }, 
                    new string[] { "def", "off", "42424" } };
data = data.OrderBy(entry => entry[0]).ToArray();  // sorts by first field
Run Code Online (Sandbox Code Playgroud)