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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:29:12+00:00 2026-06-13T03:29:12+00:00

If I have a sorting algorithm, and I want to sort by some field

  • 0

If I have a sorting algorithm, and I want to sort by some field of a structure. I’m basically looking for the possibility of a parameter (sortBy in this case) being able to determine whether fieldOne would be compared, or fieldTwo–without continually checking for the correct field to use.

void func(SomeType *arr, int length, int sortBy) {
    int i;
    for(int i = 0; i < length, i++) {
        if(sortBy == 1) {
            doSomethingTo(arr[i].fieldOne);
        }
        else if(sortBy == 2) {
            doSomethingTo(arr[i].fieldTwo);
        }
        // etc
    }
}
  • 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-13T03:29:13+00:00Added an answer on June 13, 2026 at 3:29 am

    You can observe what the Standard C qsort() does. It sorts any array of any type, using a comparator function:

    void qsort(void *base, size_t nel, size_t width,
               int (*compar)(const void *v1, const void *v2));
    

    The comparator function returns a negative value if v1 should sort before v2, a positive value if it should sort after, and zero if the values are equal under this sorting criterion. Note that it is necessary to compare two values; it is not sufficient to compare one value with its own navel.

    In your example, you are sorting a fixed type, it seems. You could use qsort(), or you can borrow the comparator type idea, and adapt it to your sort:

    void func(SomeType *arr, int length, int (*comparator)(const SomeType *v1, const SomeType *v2))
    {
        ...
        int cmp = comparator(&arr[i], &arr[j]);
        ...
    }
    

    Your comparator might be:

    static int compare_fieldOne(const SomeType *v1, const SomeType *v2)
    {
        if (v1->fieldOne < v2->fieldOne)
            return -1;
        else if (v1->fieldOne > v2->fieldOne)
            return +1;
        return 0;
    }
    

    If you need to do more comparisons, you can add extra pairs of tests after the the else if clause, leaving the equality case to the end.

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

Sidebar

Related Questions

The Heap Sort sorting algorithm seems to have a worst case complexity of O(nlogn),
Currently, I have a barebones implementation of the quicksort algorithm to sort some randomly
I have something like a sort-algorithm here, and I want to pass it a
Say, we have some items, and each defines some partial sorting rules, like this:
I need some advice when it comes to solving a sorting algorithm. This particular
I have a strong use-case to define my own sorting algorithm, which is faster
I have a problem with sorting... I need to sort NSArray containing NSDictionaries .
Is there a sorting algorithm that can sort n distinct integers from 3 to
This is another question from cracking coding interview, I still have some doubt after
I want to understand median of medians algorithm on the following example: We have

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.