Is there C# support for an index-based sort?

前端 未结 2 530
感情败类
感情败类 2020-12-19 05:09

Is there any built-in C# support for doing an index sort?

More Details:
I have several sets of data stored in individual generic Lists of double. These ar

相关标签:
2条回答
  • 2020-12-19 05:20

    Once you have set up the index array, you can sort it using a custom Comparison<T> that compares the values in the corresponding items in the data array:

    Array.Sort<int>(index, (a,b) => anylist[a].CompareTo(anylist[b]));
    
    0 讨论(0)
  • 2020-12-19 05:28

    The following code achievs an indexed sort. Note the ToArray() call to clone the data array. If omited, the data array becomes sorted, too.

    static void Main(String[] args)
    {
       Int32[] data = new Int32[] { -6, 6, 5, 4, 1, 2, 3, 0, -1, -2, -3, -4, -5 };
    
       Int32[] indices = Enumerable.Range(0, data.Length).ToArray();
    
       Array.Sort(data.ToArray(), indices);
    
       foreach (Int32 index in indices)
       {
           Console.Write(String.Format("{0} ", data[index]));
       }
    
       Console.ReadLine();
    }
    

    The output is as exspected.

    -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6
    
    0 讨论(0)
提交回复
热议问题