Sort a list of integers descending

ddanbe 0 Tallied Votes 727 Views Share

The Sort method of the List class sorts everything the default way : ascending.
You can use the Reverse method of this class to get what you want or you could implement the IComparer interface like in this snippet.

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {       
        private class SortIntDescending : IComparer<int>
        {
            int IComparer<int>.Compare(int a, int b) //implement Compare
            {              
                if (a > b)
                    return -1; //normally greater than = 1
                if (a < b)
                    return 1; // normally smaller than = -1
                else
                    return 0; // equal
            }
        }

        static List<int> intlist = new List<int>(); // make a list
        
        static void Main(string[] args)
        {
            intlist.Add(5); //fill the list with 5 ints
            intlist.Add(3);
            intlist.Add(5);
            intlist.Add(15);
            intlist.Add(7);

            Console.WriteLine("Unsorted list :");
            Printlist(intlist);

            Console.WriteLine();
            // intlist.Sort(); uses the default Comparer, which is ascending
            intlist.Sort(new SortIntDescending()); //sort descending

            Console.WriteLine("Sorted descending list :");
            Printlist(intlist);

            Console.ReadKey(); //wait for keydown
        }

        static void Printlist(List<int> L)
        {
            foreach (int i in L) //print on the console
            {
                Console.WriteLine(i);
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.