2015-10-16 19 views
-1

我有以下類:排序字符串[]的一子陣列的對象[]內

private class st_flow { 
    public string[] title; 
    public object[] details;    

    public st_flow() { 
     title= new string[0]; details = new object[0]; 
    } 
} 

st_flow.details保持一個字符串數組,我不知道數組的大小,它可以是任何地方從string[5]string[15]我的問題是我想排序這些數組。在某些情況下,我想對st_flow.details[0].mystring[6]進行排序,並在其他情況下對不同的索引進行排序。

編輯:我會盡力解釋和回答大家的評論;我的st_flow.details是一個對象,因爲它必須能夠容納任何類型的數組,每次它包含許多類型爲字符串的數組,或者int等,但從未組合過類型。所以在我的代碼,我有這樣的事情:

st_flow flow = new st_flow(); 
string[] content = new string[15]; 
flow.details = new object[15]; 
//... 
// here I fill my content and each time add it to flow 
// this part is inside a loop that each time reset the 
// content and add it to flow incrementing the index 
//... 
flow.details[index] = content; 

在這一點上,本程序的,我們將有flow.details承載數量不明的陣列他們每個人的不明大小。我們實際上並不關心任何一方的規模。試想一下:

// this contains a content[15] string array which [4] value is 50 
flow.details[0]; 
// this also contains a content[15] string arraym with [4] value 80 
flow.details[1]; 
// i need to sort on this element and be able to do it both DESC or ASC 

我需要重新梳理我flow.details取決於(例如)content[4]不管它是字符串的值(列)或INT且不論數組的大小。希望澄清我的問題,謝謝。

+0

但是....你不能爲了一個單一的元素,您要訂購和基於什麼什麼收藏? – frikinside

+2

'在某些情況下,我想對st_flow.details [0] .mystring [6]進行排序 - 這沒有多大意義。 'details [0]'只是一個'object',它不會有一個屬性'mystring'。即使它確實如何「排序」呢? – Jamiec

+0

'public stats(){'看起來像一個構造函數,但我們在'st_flow'類中:一個錯字? –

回答

0

我認爲我已經解決了這種方式,還在測試它是否適用於所有情況:

 public class flow_dt : DataTable { 
      public flow_dt(string[] columns) { 
       this.Clear(); 
       foreach (string s in columns) { 
        this.Columns.Add(s, typeof(string)); 
       } 
      }    
     } 

這樣我有標題和一個單一的元素的數據,並沒有更多的陣列,我可以排序,甚至更輕鬆地篩選它,因爲說我還在測試它

編輯:在這種情況下,我無法像這樣排序的:

  DataView dv = flow.DefaultView; 
      dv.Sort = "total DESC"; 
      flow = (flow_dt)dv.ToTable(); 

我得到一個錯誤,因爲不能執行轉換,我不明白爲什麼,因爲我的類繼承自DataTable類型。

編輯2:這是排序部分解決方法:http://bytes.com/topic/visual-basic-net/insights/890896-how-add-sortable-functionallity-datatable

1

那麼,在你的編輯的情況下,只是測試爲String[]和排序:

Object[] details = new Object[] { 
    123, 
    new String[] {"x", "a", "y"},  // This String[] array 
    "bla-bla-bla", 
    new String[] {"e", "f", "d", "a"}, // and this one will be sorted 
    }; 

...

foreach (var item in details) { 
    String[] array = item as String[]; 

    if (null != array) 
     Array.Sort(array); 
    } 

...

// Test: print out sorted String[] within details 
Console.Write(String.Join(Environment.NewLine, details 
    .OfType<String[]>() 
    .Select(item => String.Join(", ", item)))); 

測試輸出是(兩個字符串數組被找到並排序)

a, x, y 
    a, d, e, f 
+0

因爲我可以以這種方式主持任何類型的數組,請閱讀我的編輯,謝謝。 – elnath78

+0

@ elnath78:你的編輯*完全*改變解決方案 –

+0

是的,請看看我的解決方案是否可以工作並解決有類而不是標準對象的問題。 – elnath78