Click here to Skip to main content
Licence CPOL
First Posted 26 May 2008
Views 14,832
Bookmarked 5 times

Descending Sorted List

By | 26 May 2008 | Article
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)

About the Author

Or Shnaider

Software Developer (Senior)
PredictAd
Israel Israel

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralEasier way to do this... Pinmemberthiago barbedo7:53 20 Nov '09  
GeneralRe: Easier way to do this... PinmemberBhavanandham4:12 12 Jan '11  
GeneralUseless Pinmember leppie 23:05 26 May '08  
GeneralI disagree PinadminChris Maunder3:22 27 May '08  
GeneralRe: I disagree Pinmemberleckey4:18 27 May '08  
GeneralRe: I disagree PinadminChris Maunder4:26 27 May '08  
GeneralRe: I disagree Pinmemberleckey4:43 27 May '08  
GeneralRe: I disagree PinmemberRavi Bhavnani7:50 27 May '08  
GeneralRe: I disagree Pinmemberleckey7:53 27 May '08  
GeneralRe: I disagree Pinmember leppie 5:42 27 May '08  
GeneralRe: I disagree Pinmember leppie 5:48 27 May '08  
GeneralRe: I disagree Pinmemberor.shnaider9:35 27 May '08  
GeneralRe: I disagree Pinmember leppie 10:14 27 May '08  
GeneralRe: I disagree PinmemberOr Shnaider8:22 28 May '08  

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.

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