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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:16:28+00:00 2026-05-27T21:16:28+00:00

I have got an array of the following structs: typedef struct _my_data_ { unsigned

  • 0

I have got an array of the following structs:

typedef struct _my_data_ 
{
  unsigned int id;
  double latitude;
  double longitude;
  unsigned int content_len;
  char* name_dyn;
  char* descr_dyn;
} mydata;

and I would like to sort it ascending by the ID field. I read it is possible to sort arrays using the qsort function but I am not sure how to correctly use it when sorting structs.

  • 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-27T21:16:28+00:00Added an answer on May 27, 2026 at 9:16 pm

    You need a structure comparator function that matches the prototype of the function expected by qsort(), viz:

    int md_comparator(const void *v1, const void *v2)
    {
        const mydata *p1 = (mydata *)v1;
        const mydata *p2 = (mydata *)v2;
        if (p1->id < p2->id)
            return -1;
        else if (p1->id > p2->id)
            return +1;
        else
            return 0;
    }
    

    If you ever get to a more complex sort criterion, this is still a good basis because you can add secondary criteria using the same skeleton:

    int md_comparator(const void *v1, const void *v2)
    {
        const mydata *p1 = (mydata *)v1;
        const mydata *p2 = (mydata *)v2;
        if (p1->latitude < p2->latitude)
            return -1;
        else if (p1->latitude > p2->latitude)
            return +1;
        else if (p1->longitude < p2->longitude)
            return -1;
        else if (p1->longitude > p2->longitude)
            return +1;
        else
            return 0;
    }
    

    Clearly, this repeats for as many criteria as you need. If you need to call a function (strcmp()?) to compare values, call it once but assign the return to a local variable and use that twice:

    int md_comparator(const void *v1, const void *v2)
    {
        const mydata *p1 = (mydata *)v1;
        const mydata *p2 = (mydata *)v2;
        int rc;
        if (p1->latitude < p2->latitude)
            return -1;
        else if (p1->latitude > p2->latitude)
            return +1;
        else if (p1->longitude < p2->longitude)
            return -1;
        else if (p1->longitude > p2->longitude)
            return +1;
        else if ((rc = strcmp(p1->name_dyn, p2->name_dyn)) < 0)
            return -1;
        else if (rc > 0)
            return +1;
        else
            return 0;
    }
    

    Also, this template works when data members are unsigned integers, and it avoids overflow problems when comparing signed integers. Note that the short cut you might sometimes see, namely variations on:

    int md_comparator(const void *v1, const void *v2)   /* BAD */
    {                                                   /* BAD */
        const mydata *p1 = (mydata *)v1;                /* BAD */
        const mydata *p2 = (mydata *)v2;                /* BAD */
        return(p1->id - p2->id);                        /* BAD */
    }                                                   /* BAD */
    

    is bad if id is unsigned (the difference of two unsigned integers is never negative), and subject to overflow if the integers are signed and of large magnitude and opposite signs.

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

Sidebar

Related Questions

I have got the following array. Array ( [1] => a [2] => b
I have got the following pulled from an array. [TheBeautifulSouth/2-15 One Last Love Song.m4a]
i have have got an array of the complexe sort to store my navigation
In C, in an Unix environment (Plan9), I have got an array as memory.
I have got the following problem since the server has safe mode turned on,
I have got a 2d array called $myarray, and when I using var_dump($myarray), it
I have the following array value $outData with several columns. I am not sure
Got the simplified array working see below Following up on the complicated array to
I've got following array: private static Optreden[] optredens = { new Optreden(Editors, Main Stage,
I have got an array with several twitter tweets and want to delete all

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.