12,405,088 members (65,960 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have employed List.Sort after reading that it's algorithm is not only much easier to use but also faster than my own sorting techniques.

The benchmarks prove this true, but there is one thing that is stopping me from committing to using this method:

When the comparison is equal, the sort order is switched any way. I am not sure if this is my own fault or if it is part of the design.

My code is shown below.

```// Sort friends by rating (0 - 3)
onlineFriends.Sort(delegate(Friend f1, Friend f2) { return f2.Rating.CompareTo(f1.Rating); });

// If f1.Rating is 0 and f2.Rating is 0, they may swap places anyway
```
Posted 28-Jun-10 15:19pm

## 3 solutions

Rate this:
Please Sign up or sign in to vote.

## Solution 1

I wouldn't have expected this behaviour...
Why don't you just write in more sorting criteria then? Even if you can assign them all indexes or something. Just run your second lot of sorting criteria if the two are equally rated. That's what I'd do.
Comments
Derek R. White 28-Jun-10 22:38pm

I agree. I am going to make the name of the friend be the second sort criteria. I suppose it's more organized anyway.
dawmail333 28-Jun-10 22:56pm

Always err on the side of more organization, we need it somewhere ;)
Rate this:
Please Sign up or sign in to vote.

## Solution 2

I believe it's because of the QuickSort implementation that's internally used. This is a bit of a hack, but you could do this:

`friends.Sort(delegate(Friend f1, Friend f2) { return f2.Rating.CompareTo(f1.Rating) >= 0 ? 1 : -1; });`
Comments
Derek R. White 28-Jun-10 22:23pm

I was trying something like this, but it keeps saying there is an invalid argument. When I start debugging, it says that f1 is null. When I check to make sure f1 or f2 are not null, it is still telling me there is an invalid argument. Your method doesn't give this error, but only because you allow 0 to be a value. I am trying to force -1 if 0 is a value, and it doesn't like it..
Nishant Sivakumar 28-Jun-10 22:28pm

I think you are confusing the Rating's value with what CompareTo returns. CompareTo returns 0 when the items are identical and I am working around that to prevent it from swapping the items in that case.
Derek R. White 28-Jun-10 22:36pm

No, when 0 is replaced by 1 it yields the same effect, so I was trying to replace it with -1, but it won't allow me to. I think I'm just going to go back to my initial method..
Rate this:
Please Sign up or sign in to vote.

## Solution 3

So to anyone who cares, this is what I ended up with:
```friends.Sort(delegate(Friend f1, Friend f2) { int i = f2.Rating.CompareTo(f1.Rating); return i == 0 ? f1.FriendName.CompareTo(f2.FriendName) : i; });
```

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

Top Experts
Last 24hrsThis month
 OriginalGriff 305 Karthik Bangalore 200 Suvendu Shekhar Giri 185 ppolymorphe 180 Richard MacCutchan 164
 OriginalGriff 7,486 ppolymorphe 3,230 Karthik Bangalore 3,227 Richard MacCutchan 2,472 F-ES Sitecore 2,300

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 28 Jun 2010
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100