Solved

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

Posted on 2014-07-27
5
216 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why don't I see this table in EDMX file? 2 23
Need to pass a connection through class/Method 1 39
Class object 2 26
Run time Error 4 34
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

911 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