Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-07-27
5
Medium Priority
?
223 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 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

705 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