Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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

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
jetbet
Asked:
jetbet
  • 3
1 Solution
 
käµfm³d 👽Commented:
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
 
Raghu MutalikdesaiSenior Manager in a leading IT Services companyCommented:
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
 
jetbetAuthor Commented:
"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
 
jetbetAuthor Commented:
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
 
jetbetAuthor Commented:
Thanks for your prompt help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now