Click here to Skip to main content
12,402,241 members (71,163 online)
Click here to Skip to main content
Add your own
alternative version

Stats

29.1K views
5 bookmarked
Posted

Descending Sorted List

, 26 May 2008 CPOL
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

You may also be interested in...

Comments and Discussions

 
GeneralEasier way to do this... Pin
thiago barbedo20-Nov-09 7:53
memberthiago barbedo20-Nov-09 7:53 
GeneralRe: Easier way to do this... Pin
Bhavanandham12-Jan-11 4:12
memberBhavanandham12-Jan-11 4:12 
GeneralUseless Pin
leppie 26-May-08 23:05
member leppie 26-May-08 23:05 
GeneralI disagree Pin
Chris Maunder27-May-08 3:22
adminChris Maunder27-May-08 3:22 
GeneralRe: I disagree Pin
leckey27-May-08 4:18
memberleckey27-May-08 4:18 
GeneralRe: I disagree Pin
Chris Maunder27-May-08 4:26
adminChris Maunder27-May-08 4:26 
GeneralRe: I disagree Pin
leckey27-May-08 4:43
memberleckey27-May-08 4:43 
GeneralRe: I disagree Pin
Ravi Bhavnani27-May-08 7:50
memberRavi Bhavnani27-May-08 7:50 
GeneralRe: I disagree Pin
leckey27-May-08 7:53
memberleckey27-May-08 7:53 
GeneralRe: I disagree Pin
leppie 27-May-08 5:42
member leppie 27-May-08 5:42 
GeneralRe: I disagree Pin
leppie 27-May-08 5:48
member leppie 27-May-08 5:48 
GeneralRe: I disagree Pin
or.shnaider27-May-08 9:35
memberor.shnaider27-May-08 9:35 
GeneralRe: I disagree Pin
leppie 27-May-08 10:14
member leppie 27-May-08 10:14 
GeneralRe: I disagree Pin
Or Shnaider28-May-08 8:22
memberOr 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.

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