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

前端 未结 2 615
感情败类
感情败类 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: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
    

提交回复
热议问题