【问题标题】:Sorting a list<T> by index using an array使用数组按索引对 list<T> 进行排序
【发布时间】:2012-12-06 21:44:03
【问题描述】:

我在使用数组作为排序源按索引对列表进行排序时遇到问题。

假设我的班级有 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&lt;Tests&gt; myTests = new List&lt;Tests&gt;; 不会编译。你是说new List&lt;Tests&gt;(); 还是new List&lt;Tests&gt;(myInt);
  • 你能给我们举一个更复杂的例子吗?无论您将myInt[i] 解释为“列表中我希望ith 元素结束的位置”还是“我想放置在位置i 的元素的原始位置”,您的示例都会给出相同的输出在输出中”(即myInt[0] = 2 - 这是说“将记录 2 放在位置 0”还是“用记录 0 填充位置 2”?)
  • 如果您发布已编译的代码,或者更紧密地匹配您的问题,回答您的问题会容易得多。
  • 对不起,我首先遇到了代码编译问题,这就是我在这里的原因。但是,由于 Rawlings cmets,我已经意识到我做错了什么以及为什么它不起作用。但是,我已经意识到一种更简单且可能更高效的方法,只需对原始记录进行 LINQ 查询,而不是重新排列代表它们的对象。感谢大家的帮助,谢谢!

标签: c# arrays list


【解决方案1】:

在我的脑海中,这样的事情应该可以解决问题:

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

嗯。事实上,使用 Linq-to-objects 会更容易一些:

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

【讨论】:

  • 或者可能是.Select((index, x)...,这取决于 OP 希望它以哪种方式工作。 (请注意,在此示例中,OrderBy 是不必要的,因为 sortIndex 值已经按顺序排列。)
  • @Rawling 确实...我添加了一个相当简单的版本!
  • 谢谢,我实际上无法让您的代码工作,但它为我指明了正确的方向。我最终只是使用数组作为搜索值进行了 LINQ 查询。我感谢您的帮助。谢谢。
【解决方案2】:

您应该将 myTests 的值分配给另一个列表

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

【讨论】:

    【解决方案3】:

    第一:

    您不能使用myList[i],除非您在i 位置创建了列表项。

    第二:

    您没有调用 Tests 构造函数来创建新的 Tests 对象

    第三:

    您正在为 myInt[i] 分配一个空引用 myTests[i]

    你应该有类似的东西:

    for (int i = 0; i < 4; i++) {
    
        myTests.Add(new Tests(i, "foo"))
    
    }
    

    【讨论】:

      【解决方案4】:

      您提供的代码有点模糊,所以我编造了几个不同的场景。
      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();
          }
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-12
        • 2011-12-17
        • 2022-10-14
        • 1970-01-01
        • 1970-01-01
        • 2015-08-27
        相关资源
        最近更新 更多