Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strike> It's ok but I'd prefer a simpler syntax :</p> <pre><code>double[] x = { 2, 3, 1, 5, 7, 2, 3 }; double[] y = { 2, 3, 1, 5, 7, 2, 3 }; double[] x_sorted = x.OrderBy(d =&gt; d).ToArray(); double[] y_sorted = y.OrderBy(d =&gt; d).ToArray(); </code></pre> <p></strike></p> <p><strong>Edit:</strong></p> <p>Argh... I failed to spot that this was an associative array sort.</p> <pre><code>double[] x = { 2, 3, 1, 5, 7, 2, 3 }; double[] y = { 1, 2, 3, 4, 5, 6, 7 }; double[] y_sorted = y.Clone() as double[]; double[] x_sorted = x.Clone() as double[]; Array.Sort(x_sorted, y_sorted); </code></pre> <p><strong>Edit 2 1/2</strong></p> <p>And some performance tests :</p> <pre><code>public class Program { delegate void SortMethod(double[] x, double[] y); private const int ARRAY_SIZE = 3000000; private static Random RandomNumberGenerator = new Random(); private static double[] x = GenerateTestData(ARRAY_SIZE); private static double[] y = GenerateTestData(ARRAY_SIZE); private static double[] GenerateTestData(int count) { var data = new double[count]; for (var i = 0; i &lt; count; i++) { data[i] = RandomNumberGenerator.NextDouble(); } return data; } private static void SortMethod1(double[] x, double[] y) { Array.Sort(x, y); } private static void SortMethod2(double[] x, double[] y) { IEnumerable&lt;int&gt; range = Enumerable.Range(0, x.Length); x = (from n in range orderby x[n] select y[n]).ToArray(); y = (from n in range orderby x[n] select x[n]).ToArray(); } private static void SortMethod3(double[] x, double[] y) { int[] x_index = Enumerable.Range(0, x.Length).OrderBy(i =&gt; x[i]).ToArray(); x = x_index.Select(i =&gt; x[i]).ToArray(); y = x_index.Select(i =&gt; y[i]).ToArray(); } private static void SortMethod4(double[] x, double[] y) { int[] range = Enumerable.Range(0, x.Length).OrderBy(i =&gt; x[i]).ToArray(); var q = ( from n in range orderby x[n] select new { First = x[n], Second = y[n] }).ToArray(); x = q.Select(t =&gt; t.First).ToArray(); y = q.Select(t =&gt; t.Second).ToArray(); } private static void SortMethodPerformanceTest(SortMethod sortMethod) { double[] y_sorted = y.Clone() as double[]; double[] x_sorted = x.Clone() as double[]; var sw = new Stopwatch(); sw.Start(); sortMethod.Invoke(x_sorted, y_sorted); sw.Stop(); Console.WriteLine( string.Format( "{0} : {1}", sortMethod.Method.Name, sw.Elapsed)); } static void Main(string[] args) { Console.WriteLine("For array length : " + ARRAY_SIZE); Console.WriteLine("------------------------------"); SortMethodPerformanceTest(SortMethod1); SortMethodPerformanceTest(SortMethod2); SortMethodPerformanceTest(SortMethod3); SortMethodPerformanceTest(SortMethod4); Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } } </code></pre> <p>And the results : </p> <pre> For array length : 3000000 ------------------------------ SortMethod1 : 00:00:00.6088503 // Array.Sort(Array, Array) SortMethod2 : 00:00:07.9583779 // Original SortMethod3 : 00:00:04.5023336 // dtb's Linq Alternative SortMethod4 : 00:00:06.6115911 // Christian's Linq Alternative </pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload