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 6200677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:24:45+00:00 2026-05-24T04:24:45+00:00

Why is the following code Array.Sort(values); Array.Reverse(values); much faster at sorting an array in

  • 0

Why is the following code

Array.Sort(values);
Array.Reverse(values);

much faster at sorting an array in descending order compared to

Array.Sort(values, (a,b)=>(-a.CompareTo(b)));

Code was run in Release mode outside of the debugger.

What is the most efficient way to produce a descending sort for arrays, preferably in a one liner?

  • 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-24T04:24:47+00:00Added an answer on May 24, 2026 at 4:24 am

    That’s a great question. I bet your values array is an array of primitive type!

    It’s really the sort that is dominant here, because the complexity of Reverse is O(n), while the sort is O(n logn).

    The thing is that when sorting primitive types, .NET actually calls a native function, which is extremely fast – much faster that using a Comparison or Comparator.

    The function is called TrySZSort:

    [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
    [SecurityCritical]
    [MethodImpl(MethodImplOptions.InternalCall)]
    private static bool TrySZSort(Array keys, Array items, int left, int right);
    

    and here is how it’s called in the Array class:

    [ReliabilityContract(Consistency.MayCorruptInstance, Cer.MayFail)]
    [SecuritySafeCritical]
    public static void Sort<T>(T[] array, int index, int length, IComparer<T> comparer)
    {
      if (array == null)
        throw new ArgumentNullException("array");
      else if (index < 0 || length < 0)
        throw new ArgumentOutOfRangeException(length < 0 ? "length" : "index", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
      else if (array.Length - index < length)
        throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
      else if (length > 1 && (comparer != null && comparer != Comparer<T>.Default || !Array.TrySZSort((Array) array, (Array) null, index, index + length - 1)))
        ArraySortHelper<T>.Default.Sort(array, index, length, comparer);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does the following code sort this array to be in numerical order? var
An array arrives with some or all of the following values, in any order.
I have the following code. I want to add/remove check-box values to an array
I have the following code that pushes check-box values to an array. I want
The following code gets a 'report line' as an array and uses fputcsv to
In the following code upto what scope the anonymous array referred by $ref is
I use the following code try to create an array of string vectors, I
I'm using the following code to get an array with all sub directories from
In the following code: //anArray is a Array of Dictionary with 5 objs. //here
This following code gets this compile error: " invalid types 'int[int]' for array subscript

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.