Solved

C# List<object> Sort. Treat "0" as high value

Posted on 2014-07-27
5
213 Views
Last Modified: 2014-07-28
I have a list of objects I need to sort by a property.
If the 2 values are the same I then resort by a second property and so on.

This is reasonable simple but I have run into an issue regarding "0" needing to be treated as the highest value as this is just a default.

The sorting I am doing at this stage is
if (sortType == "win odds")
                {
                    runners.Sort(delegate(RunnerDetail r1, RunnerDetail r2)
                    {
                        int xdiff1 = ConvertFromString((String)r1.winTable["NZL"]).CompareTo((ConvertFromString((String)r2.winTable["NZL"])));
                        if (xdiff1 != 0)
                        {
                            return xdiff1;
                        }
                        else
                        {
                            int xdiff2 = ConvertFromString((String)r1.placeTable["NZL"]).CompareTo((ConvertFromString((String)r2.placeTable["NZL"])));
                            if (xdiff2 != 0)
                            {
                                return xdiff2;
                            }
                            else
                            {
                                return r1.number.CompareTo(r2.number);
                            };
                        };
                    });
                }

Open in new window


I cannot hack the values to a high number for the sort.
Also just copying the values into 2 lists and adding the "0" ones to the end does not work as one of the secondary values may also be "0" etc.

Can anyone help?
0
Comment
Question by:jetbet
  • 3
5 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40223456
If you find a "0" in the property belonging to the r1 item, then return 1; otherwise if you find a "0" in the property belonging to r2, then return -1.
0
 
LVL 7

Expert Comment

by:Raghu Mutalikdesai
ID: 40223476
Why not use int.MaxValue instead of 0? I don't suppose you will have other values equal to int.MaxValue. You can do the high sorting then.
0
 

Author Comment

by:jetbet
ID: 40225362
"If you find a "0" in the property belonging to the r1 item, then return 1; otherwise if you find a "0" in the property belonging to r2, then return -1."

Kaufmed: This is a good start but I need to cater for when they are both "0" to ensure the next level of sorting is engaged. I am looking at this now.
0
 

Author Comment

by:jetbet
ID: 40225378
Final Code
runners.Sort(delegate(RunnerDetail r1, RunnerDetail r2)
                    {
                        String win1 = (String)r1.winTable["NZL"];
                        String win2 = (String)r2.winTable["NZL"];
                        if ((win1 == "0") && (win2 != "0"))
                        {
                            return 1;
                        }
                        else if ((win1 != "0") && (win2 == "0"))
                        {
                            return -1;
                        }
                        else
                        {
                            int xdiff1 = ConvertFromString(win1).CompareTo(ConvertFromString(win2));
                            if (xdiff1 != 0)
                            {
                                return xdiff1;
                            }
                            else
                            {
                                String place1 = (String)r1.placeTable["NZL"];
                                String place2 = (String)r2.placeTable["NZL"];
                                if ((place1 == "0") && (place2 != "0"))
                                {
                                    return 1;
                                }
                                else if ((place1 != "0") && (place2 == "0"))
                                {
                                    return -1;
                                }
                                else
                                {
                                    int xdiff2 = ConvertFromString(place1).CompareTo(ConvertFromString(place2));
                                    if (xdiff2 != 0)
                                    {
                                        return xdiff2;
                                    }
                                    else
                                    {
                                        return r1.number.CompareTo(r2.number);
                                    };
                                }
                            };
                        }
                    });

Open in new window


Thanks for your help
0
 

Author Closing Comment

by:jetbet
ID: 40225381
Thanks for your prompt help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now