Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 275431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:45:53+00:00 2026-05-12T00:45:53+00:00

Premise This problem has a known solution (shown below actually), I’m just wondering if

  • 0

Premise

This problem has a known solution (shown below actually), I’m just wondering if anyone has a more elegant algorithm or any other ideas/suggestions on how to make this more readable, efficient, or robust.

Background

I have a list of sports competitions that I need to sort in an array. Due to the nature of this array’s population, 95% of the time the list will be pre sorted, so I use an improved bubble sort algorithm to sort it (since it approaches O(n) with nearly sorted lists).

The bubble sort has a helper function called CompareCompetitions that compares two competitions and returns >0 if comp1 is greater, <0 if comp2 is greater, 0 if the two are equal. The competitions are compared first by a priority field, then by game start time, and then by Home Team Name.

The priority field is the trick to this problem. It is an int that holds a positve value or 0. They are sorted with 1 being first, 2 being second, and so on with the exception that 0 or invalid values are always last.
e.g. the list of priorities
0, 0, 0, 2, 3, 1, 3, 0
would be sorted as
1, 2, 3, 3, 0, 0, 0, 0

The other little quirk, and this is important to the question, is that 95% of the time, priority will be it’s default 0, because it is only changed if the user wants to manually change the sort order, which is rarely. So the most frequent case in the compare function is that priorities are equal and 0.

The Code

This is my existing compare algorithm.

int CompareCompetitions(const SWI_COMPETITION &comp1,const SWI_COMPETITION &comp2)
{

    if(comp1.nPriority == comp2.nPriority)
    {
        //Priorities equal
        //Compare start time
        int ret = comp1.sStartTime24Hrs.CompareNoCase(comp2.sStartTime24Hrs);
        if(ret != 0)
        {
            return ret; //return compare result
        }else
        {
            //Equal so far
            //Compare Home team Name
            ret = comp1.sHLongName.CompareNoCase(comp2.sHLongName);
            return ret;//Home team name is last field to sort by, return that value
        }
    }
    else if(comp1.nPriority > comp2.nPriority)
    {
        if(comp2.nPriority <= 0)
            return -1;
        else
            return 1;//comp1 has lower priority
    }else /*(comp1.nPriority < comp2.nPriority)*/
    {
        if(comp1.nPriority <= 0)
            return 1;
        else
            return -1;//comp1 one has higher priority
    }
}

Question

How can this algorithm be improved?
And more importantly…
Is there a better way to force 0 to the back of the sort order?

I want to emphasize that this code seems to work just fine, but I am wondering if there is a more elegant or efficient algorithm that anyone can suggest. Remember that nPriority will almost always be 0, and the competitions will usually sort by start time or home team name, but priority must always override the other two.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:45:53+00:00Added an answer on May 12, 2026 at 12:45 am

    Isn’t it just this?

    if (a==b) return other_data_compare(a, b);
    if (a==0) return 1;
    if (b==0) return -1;
    return a - b;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

Premise This problem has a known solution (shown below actually), I'm just wondering if
I am having an issue with SSH hanging on my Mac Book Pro. This
The premise: Get a dropdownlist's content based on the value selected in the first
This question has been asked before but 1) the user never accepted an answer
I am trying to plot a bunch of data points (many thousands) in Python

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.