对字符串数组进行排序

问题描述:

我从Web服务调用发送如下的ArrayList:对字符串数组进行排序

private ArrayList testList = new ArrayList();

将存储像值:

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

我用两个这一点,因为原因:

1:我要取在ASP.NET 1.1框架此值。 2:我使用testList.Sort();我使用testList.Sort();我使用testList.Sort();}我使用testList.Sort();发送之前。

但现在我想给这些值:

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

所以我发现如下的方式:

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

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

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

abc 
def 
xyz 

你写,你必须阅读在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 
+0

是的,你的假设是正确的。发送网站是4.0 – James

+0

只是想知道这个解决方案与我的不同吗?另外注释它是错误的,因为排序是由数组项目,而不是字段 –

+0

关闭问题 - 我在循环中添加这些字符串数组... ...像这样//将每个信息添加到字符串数组。 data = new string [3]; data [0] =“xyz”; data [1] =“8787”; data [2] =“爸爸”; //将每一行添加到数组列表中。 arrList.Add(data); //将数组列表添加到属性对象。 vinDescription.ShowOptionalEquipment =(string [] [])arrList.ToArray(typeof(string []));'...这是正确的还是更好的方法? – James

只是有点内阵列的第一个项目阵列

string[][] sorted = data.OrderBy(array => array[0]).ToArray(); 
+0

会是怎样的** **分类的类型? – James

+0

字符串数组数组或字符串[] []'。我用'var'只是缩短了 –