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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:17:39+00:00 2026-06-08T17:17:39+00:00

Normally sorting Array of structure objects is easy. Consider an Array of a structure

  • 0

Normally sorting Array of structure objects is easy. Consider an Array of a structure (AOS)

#define ITEMS 10

typedef struct MyStruct
{
 char a;
 int  b;
}tMyStruct;

tMyStruct arr_mystruct[ITEMS];

I first fill this array of structure with values of < a , b> pairs.

If I now want to sort this array of structures according to the integer field, I can do it using libc qsort function by using a comparison function which takes two integer arguments.

Now consider that I replace the above structure in AOS format with SOA format

#define ITEMS 10

typedef struct MyStruct
{
 char a[ITEMS];
 int  b[ITEMS];
}tMyStruct;

tMyStruct mystruct;

Now I can still use qsort to sort the array of integers b field, however this time I need to additionally sort a (array of characters) w.r.t sorting order of b.

So my question is what is a possible efficient way to do sorting for data laid out in SOA format instead of usual AOS format?

Can anyone help me with this ? Thanks!

  • 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-08T17:17:40+00:00Added an answer on June 8, 2026 at 5:17 pm

    Probably the best way would be to use a helper array:

    int helper[ITEMS];
    
    int helper_cmp(const void *a, const void *b);
    
    for (i = 0; i < ITEMS; ++i)
        helper[i] = i;
    qsort(helper, ITEMS, sizeof(*helper), helper_cmp);
    

    The helper_cmp function will take helper values as indices to mystruct.b and compares those values:

    int helper_cmp(const void *a, const void *b)
    {
        int first = *(const int *)a;
        int second = *(const int *)b;
        int b_first = mystruct.b[first];
        int b_second = mystruct.b[second];
        // compare b_first and b_second
    }
    

    Then, after the qsort, the helper array will contain rearranged indices of b, telling how it should be ordered.

    You can use this array to both rearrange mystruct.a and mystruct.b.


    Note that this is not as efficient as sorting “array of struct”. I don’t know your application, but my guess is that “struct of array”, in which the fields of the arrays are correlated is not a good idea in the first place. That is, if sorting of b affects sorting of a, they probably belong to the same conceptual element (call it class if you want), and it is better if they are grouped together in one struct.

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

Sidebar

Related Questions

Normally to realized linked objects, i usually use getter and setter methods and this
Normally sorting based on keys and then iterating a hash can be done as
Normally, this would be incredibly easy with tables, but everyone says tables are bad
Normally, in SBC running RTOS, it would be an easy task to write data/event
Normally you make a call to a service, it blocks until a thread in
Normally, you don't actually edit a file in vim. If you run vim foo
Normally all the document related to an iOS application are stored in a document
Normally when I want to change the connection for a Query Editor Window in
Normally, if I want to start a new activity I can use StartActivity(typeof(foo)); This
Normally the XML files I have to parse are like this: <row id=1> <title>widget<title>

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.