Click here to Skip to main content
Click here to Skip to main content

Descending Sorted List

, 26 May 2008
Rate this:
Please Sign up or sign in to vote.
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:

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:

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:

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:

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)

Share

About the Author

Or Shnaider
Software Developer (Senior) PredictAd
Israel Israel
No Biography provided

Comments and Discussions

 
GeneralEasier way to do this... Pinmemberthiago barbedo20-Nov-09 7:53 
GeneralRe: Easier way to do this... PinmemberBhavanandham12-Jan-11 4:12 
GeneralUseless Pinmember leppie 26-May-08 23:05 
GeneralI disagree PinadminChris Maunder27-May-08 3:22 
GeneralRe: I disagree Pinmemberleckey27-May-08 4:18 
GeneralRe: I disagree PinadminChris Maunder27-May-08 4:26 
GeneralRe: I disagree Pinmemberleckey27-May-08 4:43 
GeneralRe: I disagree PinmemberRavi Bhavnani27-May-08 7:50 
GeneralRe: I disagree Pinmemberleckey27-May-08 7:53 
GeneralRe: I disagree Pinmember leppie 27-May-08 5:42 
GeneralRe: I disagree Pinmember leppie 27-May-08 5:48 
GeneralRe: I disagree Pinmemberor.shnaider27-May-08 9:35 
GeneralRe: I disagree Pinmember leppie 27-May-08 10:14 
GeneralRe: I disagree PinmemberOr Shnaider28-May-08 8:22 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 27 May 2008
Article Copyright 2008 by Or Shnaider
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid