Sorting a C array based on contents of another array?

If you want to use qsort the best thing to-do would be to re-wrap the indexes in A and the values in B into a struct, and then make a comparator based on a new array that struct. For instance: typedef struct { int index_from_A; int value_from_B; } index_value_wrapper; index_value_wrapper index_wrapper_array5; for (int i=0; I Value_from_B = BAi; } int comparitor (const void* lhs, const void* rhs) { return (lhs. Value_from_B - rhs.

Value_from_B); } Now you can run qsort on the struct array and from there you can extract the proper sorted sequence you desired for the original array A without having to use a custom sorting function.

If you want to use qsort, the best thing to-do would be to re-wrap the indexes in A and the values in B into a struct, and then make a comparator based on a new array that struct. For instance: typedef struct { int index_from_A; int value_from_B; } index_value_wrapper; index_value_wrapper index_wrapper_array5; for (int i=0; I Value_from_B = BAi; } int comparitor (const void* lhs, const void* rhs) { return (lhs. Value_from_B - rhs.

Value_from_B); } Now you can run qsort on the struct array and from there you can extract the proper sorted sequence you desired for the original array A without having to use a custom sorting function.

If you have it available, qsort_r provides a way to do this. You can give it context information in an additional parameter. That context is passed to the comparison function.

You can access that additional information to extract the desired sorting information. The Microsoft compiler has a similar one: qsort_s.

I'm using Ubuntu and it doesn't seem to be around... – ajwood May 20 at 19:47.

I think you can use qsort and a custom comparator int comparator(const void *x, const void *y) { return ( b*(int*)x - b*(int*)y ); }.

– ajwood May 20 at 19:34 @ajwood I am unsure I understand your question – cnicutar May 20 at 19:34 Ajwood, If you mean the array indices, no - the array elements move around as the sort progresses – Paul May 20 at 19:37 @cnicutar: Your answer would be perfect... but these arrays are local variables in a function, so the comparator can't check values of B – ajwood May 20 at 19:38 @ajwood: From what I can see, in the code snipped above, be would have to be some type of globally accessible array. – Jason May 20 at 19:38.

Create another array C of type struct { int a_value; int b_value}, initialise each element to the values of each index of a and the value looked up from b. Sort that, traverse the sorted C copying the a_values back into A. Viola.No, that's a large violin.

Voila!

Use your rule as the comparison function to qsort (as long as B is longer than A): #include #include int A = {0, 1, 4, 5, 7}; int B= {5, 3, 8, 2, 2, 7, 1, 6, 3, 9}; int my_cmp(const void *a_, const void *b_,void *arg_) { const int *a = a_, *b = b_; if(B*a == B*b) return 0; else if (B*a Using that, you'd change the comparison function to e.g. Int my_cmp(const void *a_, const void *b_,void *arg_) { const int *a = a_, *b = b_, *arg = arg_; if(arg*a == arg*b) return 0; else if (arg*a.

Doesn't work if A and B are local to main. – Paul May 20 at 19:47 This works well as long as it is not multi-threaded. If it is not single-threaded and the reference data (B) is not static, then it would not work.

– Mark Wilkins May 20 at 19:48 @Paul qsort is the only standard sorting function in C and it works like that, otherwise you'll have to roll your own , or use something non-standard, like qsort_r - added an example for that. Unless you're in a multithreaded environment, A and B can very well be local. You just need to set a global int * to your B array before calling qsort – nos May 20 at 19:55 @nos, standard or not if it doesn't work you can't use it :) A global works (although it's a bit of a code smell) or use a wrapper struct as others (including me) have suggested.

– Paul May 20 at 20:02 @Paul qsort_r works fine on the platforms that have it. – nos May 207 at 11:50.

I'm trying to sort an array A whose elements are indexes. The indexes refer to another array B whose value will determine the order of A. So, I would like to sort A such that B Ai is increasing.

Is this possible with C's built-in sort, or am I going to have to write my own implementation? EDIT: These arrays are local function variables.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions