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

  • Home
  • SEARCH
  • 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 9123015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:14:18+00:00 2026-06-17T06:14:18+00:00

I have got a 2d array and I would like to quicksort it with

  • 0

I have got a 2d array and I would like to quicksort it with the given qsort() function in C++:

unsigned work[N][3];

I would like to sort the “work” array by the third index… so if work[i] goes before work[j] if work[i][2]>work[j][2].

I know I would need to use a function to compare it, but I have no inkling how to do that.

edit:
If I would do the following, would that help:

unsigned work[3][N];
qsort(work[2], N, sizeof(unsigned), compare);

And compare would be the following:

int compare(const void* a, const void* b)
{
    return(*(unsigned*)a-*(unsigned*)b);
}

?

  • 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-06-17T06:14:19+00:00Added an answer on June 17, 2026 at 6:14 am

    Well, the short answer would be to not use std::qsort at all, but std::sort. But unfortunately the latter won’t work, since unsigned int[3] is not assignable. So here’s the easiest std::qsort solution.

    First we define a custom comparator function:

    // return -1 if a before b, 1 if after, 0 if equal
    int compare(const void *a, const void *b)
    {
        const unsigned int *arg1 = reinterpret_cast<const unsigned int*>(a);
        const unsigned int *arg2 = reinterpret_cast<const unsigned int*>(b);
        if(arg1[2] > arg2[2])
            return -1;
        if(arg1[2] < arg2[2])
            return 1;
        return 0;
    }
    

    Which we then use to sort the array. Keep in mind that work is an array of arrays, and thus work[0] is an array of 3 unsigned ints, there’s no pointer indirection involved in any way. So it’s perfectly suited for being sorted by std::qsort:

    std::qsort(work, sizeof(work)/sizeof(work[0]), sizeof(work[0]), compare);
    

    By the way, the third element is indexed with 2, since we usually start to count at 0 in C++ (and many other programming languages).

    EDIT: Though, the best solution would indeed be to drop this array of arrays and use something more suited to C++, like a std::vector of std::array<unsigned int,3>s (or any other datastructure that fits a bit more to the actual context):

    typedef std::array<unsigned int,3> uint3;
    std::vector<uint3> work(N);
    

    Which can then be sorted with a simple:

    std::sort(std::begin(work), std::end(work), 
              [](const uint3 &a, const uint3 &b) { return a[2] > b[2]; });
    

    Or, if you don’t have C++11 (though in this case you won’t have std::array either and need to start thinking about a resonable datastructure apart from a mere 3-array):

    struct compare
    {
        bool operator()(const uint3 &a, const uint3 &b) const
        {
            return a[2] > b[2];
        }
    };
    
    std::sort(work.begin(), work.end(), compare());
    

    As a bonus to much clearer code, you also most probably get a slight performance boost of std::sort over std::qsort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have have got an array of the complexe sort to store my navigation
I have an array of doubles and a threshold value. I would like to
I have an array of terms that I would like to match using the
I've got a byte array that was created using a hash function. I would
I got again tiny problem I would like to store strings in array I
I have a an array that I populate that I would in turn like
I have the numbers 1000 to 9999. I would like to have a function
I have got an array of the following structs: typedef struct _my_data_ { unsigned
I've got an array of names: $names = array('ray'=>0,'bob'=>1,'sue'=>2,'jeff'=>3); Then I have a table
I'm having some problems building an hierarchistic array structure - I have almost got

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.