Click here to Skip to main content
15,896,278 members
Articles / Programming Languages / C#
Article

Descending Sorted List

Rate me:
Please Sign up or sign in to vote.
2.75/5 (3 votes)
26 May 2008CPOL 42.1K   5   14
What if you want the object to be sorted in descending order?

Introduction

Microsoft gives us an out-of-the-box sorted list object which sorts its items automatically.

But what if you want the object to be sorted in descending order?

Using the Code

Create the following class:

C#
internal class DescendingComparer : IComparer
    { 
        public int Compare(object x, object y)
        {
            try
            {
                return System.Convert.ToInt32(x).CompareTo
                    (System.Convert.ToInt32(y)) * -1;
            }
            catch (Exception ex)
            {
                return x.ToString().CompareTo(y.ToString());
            }
        } 
    }

And then create the sorted list:

C#
Sorted List clsScoresDesc = new SortedList(new DescendingComparer());

But... it was still not good enough for me because my key was double, so I created the following class:

C#
internal class DoubleComparer : IComparer<double>
    {
        const double eps = 1E-10;
        public int Compare(double x, double y)
        { return y > x + eps ? 1 : y < x - eps ? -1 : 0; }
    } 

And then the sorted list:

C#
IComparer<double> doubleComparer = new DoubleComparer ();
slAdComponent = new SortedList<double, myComponent>( doubleComparer);

You can also iterate through the sorted list using the for loop upside down, but I like the IComparer.

History

  • 27th May, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) PredictAd
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralEasier way to do this... Pin
thiago barbedo20-Nov-09 7:53
thiago barbedo20-Nov-09 7:53 
GeneralRe: Easier way to do this... Pin
Bhavanandham12-Jan-11 4:12
Bhavanandham12-Jan-11 4:12 
GeneralUseless Pin
leppie26-May-08 23:05
leppie26-May-08 23:05 
GeneralI disagree Pin
Chris Maunder27-May-08 3:22
cofounderChris Maunder27-May-08 3:22 
GeneralRe: I disagree Pin
leckey27-May-08 4:18
leckey27-May-08 4:18 
GeneralRe: I disagree Pin
Chris Maunder27-May-08 4:26
cofounderChris Maunder27-May-08 4:26 
GeneralRe: I disagree Pin
leckey27-May-08 4:43
leckey27-May-08 4:43 
GeneralRe: I disagree Pin
Ravi Bhavnani27-May-08 7:50
professionalRavi Bhavnani27-May-08 7:50 
The "sniff" icon seems to have changed overnight! Or is it just me?

/ravi

My new year resolution: 2048 x 1536
Home | Articles | My .NET bits | Freeware
ravib(at)ravib(dot)com

GeneralRe: I disagree Pin
leckey27-May-08 7:53
leckey27-May-08 7:53 
GeneralRe: I disagree Pin
leppie27-May-08 5:42
leppie27-May-08 5:42 
GeneralRe: I disagree Pin
leppie27-May-08 5:48
leppie27-May-08 5:48 
GeneralRe: I disagree Pin
Or Shnaider27-May-08 9:35
Or Shnaider27-May-08 9:35 
GeneralRe: I disagree Pin
leppie27-May-08 10:14
leppie27-May-08 10:14 
GeneralRe: I disagree Pin
Or Shnaider28-May-08 8:22
Or Shnaider28-May-08 8:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.