0

I am sending a ArrayList as below from a web service call :

private ArrayList testList = new ArrayList();

Which will store values like :

"xyz (pound) (4545)"
"abc (in)    (346)"
"def (off)   (42424)"

I use this because of two reasons :

1 : I have to fetch this value in ASP.NET 1.1 framework.

2 : I use testList.Sort(); before sending.

But now I want to send these values as :

"xyz"   "pound"  "4545"
"abc"  "in"    "346"
"def"   "off" "42424"

So I found a way as below :

string[][] data = { new string[]{"xyz", "pound", "4545"},
                    new string[]{"abc", "in", "346"}, 
                    new string[]{"def", "off", "42424"}};

Question is : How can I sort it effectively?? OR Is there a better way to address this ?

Sort will be done based on first element :

abc
def
xyz
4

3 回答 3

5

您写道,您必须在 ASP 1.1 中读取此值,因此我假设您在发送端有一个更现代的 .NET 框架版本。

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

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
于 2012-10-19T13:15:54.653 回答
2

只需按数组中的第一项对内部数组进行排序

string[][] sorted = data.OrderBy(array => array[0]).ToArray();
于 2012-10-19T13:15:09.470 回答
0

仅当我们尝试比较字符串的第一个字母时,接受的答案才是正确的。我有类似的问题并且要获得准确的排序,您需要按整个字符串排序,而不仅仅是第一个字符。

data = data.OrderBy(entry => entry).ToArray(); // sorts by first field

于 2018-10-31T14:52:36.970 回答