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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:09:11+00:00 2026-05-27T04:09:11+00:00

In Following Code , What will occur in Sort Method Of Main method ,

  • 0

In Following Code , What will occur in Sort Method Of Main method , Does it contain a loop on it?

I have found it uses QuickSort Algorithm but How it Sort By Compare Function?

class Products
    {
        public string Name { get; private set; }
        public decimal Price { get; private set; }
        public Products(string name,decimal price)
        {
            Name = name;
            Price = price;
        }
        public Products()
        {

        }
        public static List<Products> GetSampleProducts()
        {
            return new List<Products>()
            {
                new Products{Name = "Company",Price=1800},
                new Products{Name = "Assassins",Price=2800},
                new Products{Name = "Zrogs",Price=1300},
                new Products{Name = "Swenney Todd",Price=1300}
            };
        }
        public override string ToString()
        {
            return string.Format("{0} price is {1}", Name, Price);
        }
    }
    class ProductComparer : IComparer<Products>
    {
        public int Compare(Products x, Products y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Products products = new Products();
            List<Products> lst_products = Products.GetSampleProducts();
            lst_products.Sort (new ProductComparer());
            foreach (Products p in lst_products)
            {
                Console.WriteLine(p);
            }
            Console.ReadKey();
        }
    }

Thanks…

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

    In general it uses Array.Sort() method and comparer you’ve provided to compare each next two items whilst sorting uisng QuickSort algorithm.

    Funny animated QuickSort:

    Visualization of the quicksort algorithm. The horizontal lines are pivot values:
    enter image description here

    EDIT:

    Below are methods which actually does QuickSort using provided comparer (QuickSort, SwapIfGreaterWithItems)

    // internal class ArraySortHelper<T> : IArraySortHelper<T>
    internal static void QuickSort(
                             T[] keys, 
                             int left, 
                             int right, 
                             IComparer<T> comparer)
    {
        do
        {
            int a = left;
            int b = right;
            int num3 = a + ((b - a) >> 1);
            ArraySortHelper<T>.SwapIfGreaterWithItems(keys, comparer, a, num3);
            ArraySortHelper<T>.SwapIfGreaterWithItems(keys, comparer, a, b);
            ArraySortHelper<T>.SwapIfGreaterWithItems(keys, comparer, num3, b);
            T y = keys[num3];
            do
            {
                while (comparer.Compare(keys[a], y) < 0)
                {
                    a++;
                }
                while (comparer.Compare(y, keys[b]) < 0)
                {
                    b--;
                }
                if (a > b)
                {
                    break;
                }
                if (a < b)
                {
                    T local2 = keys[a];
                    keys[a] = keys[b];
                    keys[b] = local2;
                }
                a++;
                b--;
            }
            while (a <= b);
            if ((b - left) <= (right - a))
            {
                if (left < b)
                {
                    ArraySortHelper<T>.QuickSort(keys, left, b, comparer);
                }
                left = a;
            }
            else
            {
                if (a < right)
                {
                    ArraySortHelper<T>.QuickSort(keys, a, right, comparer);
                }
                right = b;
            }
        }
        while (left < right);
    }
    
    
    private static void SwapIfGreaterWithItems(
                                    T[] keys, 
                                    IComparer<T> comparer, 
                                    int a, 
                                    int b)
    {
        if ((a != b) && (comparer.Compare(keys[a], keys[b]) > 0))
        {
            T local = keys[a];
            keys[a] = keys[b];
            keys[b] = local;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code in my Application. Comments in my code will specify My
I have the following code, which will not work. The javascript gives no errors
I have the following code, which will retrieve a filename from a table and
I have the following code (cut down for readability): Main Class: public StartProcess() {
I have the following code: public static void main(String[] args) { try { int
The following code will generate a link to the page I want to get
The following code will not run correctly in IE7 with the latest service packs
The following code will search for the user within the domain controller, but I
The following code will add the categories selector widget to the WordPress Page editor
The following code will log in my application to a server. That server will

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.