Solved

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

Posted on 2014-07-27
5
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 75

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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