2012-12-06 22 views
0

我在使用數組作爲排序源按索引排序列表時出現問題。使用數組按索引對<T>排序

假設有5條設置爲我的課

class Program 
{ 
    static void Main(string[] args) 
    { 
    int[] myInt {2,1,0,3,4} 
    List<Tests> myTests = new List<Tests>; 

    //this part doesn't work 

    for (int i = 0; i < 4; i++) 
     { 
     myInt[i] = myTests[i]; 
     } 
    myTests.ForEach(i => Console.WriteLine("{0} {1}", i.id, i.myString)); 
    } 
} 

我的類定義

class Tests 
{ 
    public int iD {get; set;} 
    public string myString {get; set;} 

    public Tests (int iD, string myString) 
    { 
     this.iD = iD; 
     this.myString = myString 
    } 
{ 

我想看到什麼出來

 record 2 
    record 1 
    record 0 
    record 3 
    record 4 

我嘗試使用排序函數的列表,但我找不到任何使用數組作爲排序標準的例子,所以我有點失落。我感謝提供的任何幫助。

+2

'List myTests = new List ;'不會編譯。你的意思是'新列表();'或'新列表(myInt);'? –

+0

你能舉一個更復雜的例子嗎?無論您將myInt [i]'解釋爲「我希望第i個元素最終位於列表中的位置」還是「我想放置在位置的元素的原始位置」,您的示例都會給出相同的輸出在輸出中輸入'i'(即'myInt [0] = 2' - 這是說「將記錄2放在位置0」還是「用記錄0填充位置2」?) – Rawling

+0

如果您發佈編譯的代碼,或者更仔細地匹配您的問題,那麼回答您的問題會容易得多。 –

回答

2

關閉我的頭頂,這樣的事情應該做的伎倆:

var sortedTests = myInt 
    .Select((x,index) => new {test = myTests[x], sortIndex = index}) 
    .OrderBy(x => x.sortIndex) 
    .Select(x => x.test) 
    .ToList() 

嗯。事實上,使用LINQ到對象是比較容易:

var sortedTests = myInt 
    .Select(x => myTests[x]) 
    .ToList(); 
+0

或者可能'.Select((index,x)...'取決於OP希望這種方式工作的方式(注意,在這個例子中,'OrderBy'是不必要的,因爲'sortIndex'值已經在) – Rawling

+0

@Rawling確實......我添加了一個相當簡單的版本! – spender

+0

謝謝,我實際上無法讓你的代碼工作,但它指出我正確的方向。該數組作爲搜索值。我感謝您的幫助,謝謝。 – evilsushi

0

你應該分配myTests的值到另一個目錄

List<Tests> newTests = new List<Tests>(); 
for (int i = 0; i < 4; i++) 
{ 
    newTests.Add(myTests[myInt[i]]); 
} 
0

第一:

不能使用myList[i],除非您創建了位置爲i的列表項。

二:

你不調用構造函數Tests創建一個新的Tests對象

三:

你分配給myInt[i]空參考myTests[i]

你應該有這樣的:

for (int i = 0; i < 4; i++) { 

    myTests.Add(new Tests(i, "foo")) 

} 
0

您提供的代碼有點模糊,所以我製作了幾個不同的場景。
1.根據索引創建。
2.基於索引排序。

將其粘貼到您的IDE,它會工作。

class Program 
    { 
    static void Main(string[] args) 
    { 
     int[] myInt = new[] { 2, 1, 0, 3, 4 }; 

     // there is nothing to sort at this time, but this is how I would make a new list matching your index.... 
     var myTests = (from x in myInt select new Tests(x, "whatever")).ToList(); 

     myTests.ForEach(i => Console.WriteLine("{0} {1}", i.iD, i.myString)); 


     // So assuming that you are starting with an unsorted list.... 
     // We create and priont one.... 
     myTests = new List<Tests>(); 
     for (int i = 0; i < myInt.Length; i++) 
     { 
     myTests.Add(new Tests(i, "number " + i)); 
     } 

     Console.WriteLine("unsorted =="); 
     myTests.ForEach(i => Console.WriteLine("{0} {1}", i.iD, i.myString)); 

     // And this will perform the sort based on your criteria. 
     var sorted = (from x in myInt 
        from y in myTests 
        where y.iD == x 
        select y).ToList(); 

     // And output the results to prove it. 
     Console.WriteLine("sorted =="); 
     sorted.ForEach(i => Console.WriteLine("{0} {1}", i.iD, i.myString)); 

     Console.Read(); 
    } 
    }