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 4009918
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:56:10+00:00 2026-05-20T08:56:10+00:00

I have a table stored in contiguous memory. It needs to stay in that

  • 0

I have a table stored in contiguous memory. It needs to stay in that format after the sort.

For example:

int table[5][3] = {
    { 60, 5, 10 },
    { 0, 200, 15 },
    { 55, 50, 365 },
    { 4, 7, 78 },
    { 555, 8, 11 },
};

Except much bigger (the size of the biggest, in bytes, is approximately 27 KB). Each cell is always an int32, and all rows have the same amounts of columns.

Let’s say that I want to sort it based on the first column, so that the result must be equivalent to:

    { 0, 200, 15 },
    { 4, 7, 78 },
    { 55, 50, 365 },
    { 60, 5, 10 },
    { 555, 8, 11 },

What’s the best way to do this? I imagine there is a better way than to convert this to a std::list, call sort(), and convert back.

Also, something built into C++ where I just have to call some function would be best.

  • 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-20T08:56:11+00:00Added an answer on May 20, 2026 at 8:56 am

    std::sort won’t do it easily because arrays aren’t assignable.

    However, std::qsort will do it:

    int cmp_first_column(const void *lhs_, const void *rhs_) {
        // optimize this to taste
        const int *lhs = static_cast<const int*>(lhs_);
        const int *rhs = static_cast<const int*>(rhs_);
        if (lhs[0] < rhs[0]) return -1;
        if (lhs[0] > rhs[0]) return 1;
        return 0;
    }
    
    std::qsort(table, 5, 3*sizeof(int), cmp_first_colum);
    

    OK, so std::qsort doesn’t benefit from template inlining optimization, but it gets the job done and at least you aren’t allocating a lot of memory and doing unnecessary copying.

    You could instead look to replace your array of int[3] with an array of structs with an int[3] as a data member. That would then be assignable and you could use std::sort normally. Depends how much other code you have written that relies on the current type, and whether it’s OK to break the interface that code uses.

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

Sidebar

Related Questions

I have a stored procedure that returns values from a temp table. In my
I have a stored procedure that ideally should be able to accept a list/table
I have a table with a date column that I know is stored in
I have a table that contains information pointing to files stored on a file
I have a table that contains information pointing to files stored on a file
I have snippets of Html stored in a table. Not entire pages, no tags
I have the following 3 rails classes, which are all stored in one table,
Greetings, I have data stored on mysql with delimiter , in 1 table. I
I'm overthinking this. I have colors stored in a database table, and I want
I have a table that stores all the volunteers, and each volunteer will be

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.