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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:16:25+00:00 2026-06-06T21:16:25+00:00

Say I have a set of data points that are represented as an array

  • 0

Say I have a set of data points that are represented as an array of arrays of doubles, so

double **data;

Now if I wanted to sort the data by the some field in each of the data points, say the 2nd field, I would write a comparator that would do something like:

int compare_data_second_field(void *a, void *b) {
    double da = ((double *) a)[1];
    double db = ((double *) b)[1];
    if (da < db) return -1;
    else if (da > db) return 1;
    return 0;
}

and then use qsort to sort them by the 2nd field.

My question is how do I generalize this if I don’t know before hand which field I want to sort by? Like I may want to sort by the 1st field sometimes and the 5th field sometimes, etc. I would also like it to be thread safe so I don’t want to use a global variable to keep track of what field to sort by since multiple of these might be going at once.

In C++ I would just use a custom sorting class and have an instance variable in the class to keep track of what field to sort by. I don’t know how to do something like this in C.

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

    The best way would be to use qsort_r if it is available on your platform. qsort_r accepts an additional argument that is passed to your comparator, so you could use that to pass the field by which you want to sort your data.

    If that is not available on your platform, then there really isn’t a simple way to do this. You can work around it using global variables, wrapping your data in a struct that would contain information on the sorting field, or rolling your own qsort_r-like function.

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

Sidebar

Related Questions

I have a set of data that has two points; watts and a time
I have set up a Core Data model where I have two objects, say
I have two data tables in a data set for example lets say Servers:
Let's say I have some code like the following, and that processData gets executed
Say I have a set of Sites that have a collection of Users .
Lets say I have this data set... var a = [5,6,7]; var b =
I have a customer with a very small set of data and records that
Say I have a group of struct's that I want to manage arrays of
Lets say you have a set: foo = {1, 2, 3, 4, 5} In
let's say I have this set of HTML-markup and CSS #CSS .inputhelp_text { background:

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.