当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Sorting array of string arrays

问题描述:

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

网友答案:

You write that you have to read this value in ASP 1.1, so I assume that you have a more modern version of the .NET framework available on the sending side.

If that is the case, you can use the OrderBy method of LINQ, included in Framework 3.5 or higher:

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
网友答案:

Just sort inner arrays by first item in array

string[][] sorted = data.OrderBy(array => array[0]).ToArray();
分享给朋友:
您可能感兴趣的文章:
随机阅读: