我在使用數組作爲排序源按索引排序列表時出現問題。使用數組按索引對<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
我嘗試使用排序函數的列表,但我找不到任何使用數組作爲排序標準的例子,所以我有點失落。我感謝提供的任何幫助。
'List myTests = new List ;'不會編譯。你的意思是'新列表();'或'新列表(myInt);'? –
你能舉一個更復雜的例子嗎?無論您將myInt [i]'解釋爲「我希望第i個元素最終位於列表中的位置」還是「我想放置在位置的元素的原始位置」,您的示例都會給出相同的輸出在輸出中輸入'i'(即'myInt [0] = 2' - 這是說「將記錄2放在位置0」還是「用記錄0填充位置2」?) – Rawling
如果您發佈編譯的代碼,或者更仔細地匹配您的問題,那麼回答您的問題會容易得多。 –