2012-10-19 247 views
0

我從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 

回答

4

你寫,你必須閱讀在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

2

只是有點內陣列的第一個項目陣列

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

會是怎樣的** **分類的類型? – James

+0

字符串數組數組或字符串[] []'。我用'var'只是縮短了 –

相關問題