Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 552971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:33:21+00:00 2026-05-13T11:33:21+00:00

Is it possible to improve the efficiency of those linq requests? I use two

  • 0

Is it possible to improve the efficiency of those linq requests? I use two different loops…
Can you help me to optimize this code?

        double[] x = { 2, 3, 1, 5, 7, 2, 3 };
        double[] y = { 1, 2, 3, 4, 5, 6, 7 };            
        IEnumerable<int> range = Enumerable.Range(0, x.Length);
        double[] y_sorted = (from n in range orderby x[n] select y[n]).ToArray();
        double[] x_sorted = (from n in range orderby x[n] select x[n]).ToArray();

This code in python is like that if you prefer:

        x_index = argsort(x)
        x_sorted = [x[i] for i in x_index]
        y_sorted = [y[i] for i in x_index]

you will notice that, in this python code, i use only one sort. that’s not the case of this c# code.

we should get at the end:

        x_sorted = { 1, 2, 2, 3, 3, 5, 7 }
        y_sorted = { 3, 1, 6, 2, 7, 4, 5 }

Fred

Edit:
I use the program of Diadistis (after a small correction)

So here we go:
Array.Sort(x, y) (0.05) is the fastest way following (0.18) by

        int[] x_index = Enumerable.Range(0, x.Length).OrderBy(i => x[i]).ToArray();
        double[] x_sorted = x_index.Select(i => x[i]).ToArray();
        double[] y_sorted = x_index.Select(i => y[i]).ToArray();

The other solutions are quite equivalent (~0.35) in time consumption on my pc.

If someone have an interesting idea, I will profile it and update this post.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:33:22+00:00Added an answer on May 13, 2026 at 11:33 am


    It’s ok but I’d prefer a simpler syntax :

    double[] x = { 2, 3, 1, 5, 7, 2, 3 };
    double[] y = { 2, 3, 1, 5, 7, 2, 3 };
    
    double[] x_sorted = x.OrderBy(d => d).ToArray();
    double[] y_sorted = y.OrderBy(d => d).ToArray();
    

    Edit:

    Argh… I failed to spot that this was an associative array sort.

    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);
    

    Edit 2 1/2

    And some performance tests :

    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 < 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<int> 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 => x[i]).ToArray();
    
            x = x_index.Select(i => x[i]).ToArray();
            y = x_index.Select(i => y[i]).ToArray();
        }
    
        private static void SortMethod4(double[] x, double[] y)
        {
            int[] range =
                Enumerable.Range(0, x.Length).OrderBy(i => 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 => t.First).ToArray();
            y = q.Select(t => 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();
        }
    }
    

    And the results :

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Delete object and all its child objects in Entity Framework? This code:
I expect there are many possible solutions to this question, I can come up
Possible Duplicate: Does use of final keyword in Java improve the performance? The final
Possible Duplicate: Mysql: Perform of NOT EXISTS. Is it possible to improve permofance? Is
Possible Duplicate: What strategies have you used to improve build times on large projects?
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
I was wondering if anyone could help me with this problem: I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.