Click here to Skip to main content
15,905,867 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.2K   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 
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 
or.shnaider wrote:
Could you explain why is it wrong?


I didnt mean it would give the wrong the results Smile | :)

But the way it is done, especially in the first example is not very good.

You should have been using IComparer<double> from the start!

As you did in the first example, you learnt that the negation of the return result of Compare() will sort a list in a descending order. So your code in this case should have been a lot more simple. Example:
public int Compare(object x, object y)
{
  return -((IComparable)x).CompareTo(y);
}

As a bonus this will sort any IComparable type Smile | :) Also note the simple negation.

The second example you seem to get it right, but I am not sure why you are using your own defined epsilon value, and why you are using it in the first place. This should simple be:
public int Compare(double x, double y)
{ 
  return y.CompareTo(x);
}

Or even:
public int Compare(double x, double y)
{ 
  return (int) Math.Ceiling(y - x);
}

The last sample shows you have to be careful what you return. If you didnt have the Math.Ceiling, all values between 0.5 and -0.5 would be considered equal OMG | :OMG: Poke tongue | ;-P

Cheers

leppie

xacc.ide - now with TabsToSpaces support
IronScheme - 1.0 alpha 3 out now

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.