Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

qsort to std::sort

This topic is 2505 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had an array of cards that I sorted using the following

int sortCard(const void *x_, const void *y_)
{
Card **x = (Card **)x_;
Card **y = (Card **)y_;

int rc = -((*x)->IsSelected() - (*y)->IsSelected());

if(!rc)
rc = -((*x)->IsAnimating() - (*y)->IsAnimating());

if(!rc)
rc = -((*x)->GetZ() - (*y)->GetZ());

if(!rc)
rc = ((*x)->GetIndex() - (*y)->GetIndex());

return rc;
}

qsort(m_deck, m_deckCount * CARD_COUNT, sizeof(Card *), sortCard);


But I've changed the array to be a vector and am attempting to perform the same sort.

struct SortCard
{
int operator()(Card* a, Card* b)
{
int rc = -(a->IsSelected() - b->IsSelected());

if(!rc)
rc = -(a->IsAnimating() - b->IsAnimating());

if(!rc)
rc = -(a->GetZ() - b->GetZ());

if(!rc)
rc = (a->GetIndex() - b->GetIndex());

return rc;
}
};

std::sort(m_deck.begin(), m_deck.end(), SortCardId());


But this is not working (it crashes the DS). How can I get the same sort working?

Share this post


Link to post
Share on other sites
Advertisement
What's the type of m_deck?
Can you debug on the device?
If you can't debug on the device (really???), just comment out the code in the compare function, to see which line cause the problem?

In your qsort version the parameter is pointer to pointer, but in std::sort version it's pointer, is this the problem?

Share this post


Link to post
Share on other sites
You need to adjust your comparison function, because qsort and std::sort have different semantics with regards to the return value of the comparison function. For qsort, the return value shall return a negative, zero or a positive value if the left hand side is smaller, equal to, or greater than the right hand side, respectively. For std::sort, the function shall only return a bool, stating whether the left hand side is less than the right hand side or not. These semantics are not directly compatible.

Share this post


Link to post
Share on other sites
Unfortunately I don't have a debugger set up. I believe you can use Insight for the Nintendo DS but I don't have it set up.

This seems to have solved the problem

struct SortCard
{
bool operator()(Card* a, Card* b)
{
int rc = -(a->IsSelected() - b->IsSelected());

if(!rc)
rc = -(a->IsAnimating() - b->IsAnimating());

if(!rc)
rc = -(a->GetZ() - b->GetZ());

if(!rc)
rc = (a->GetIndex() - b->GetIndex());

return (rc < 0);
}
};


Thanks for the reply

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!