qsort mode-based sorting

  • Thread starter Dimitris Mandalidis
  • Start date
D

Dimitris Mandalidis

Hello all,
Suppose we have the following struct :

struct foo {
int a;
int b;
char *c;
}

And :

struct foo bar[100]

And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?

D.
 
J

Jens.Toerring

Dimitris Mandalidis said:
Suppose we have the following struct :
struct foo {
int a;
int b;
char *c;
}
struct foo bar[100]
And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?

When you can't use an external variable for the mode (and you don't
want to stick a 'mode' member in all structures and keep that ip-to-
date) then you need to write your own qsort(). On the other hand, why
don't you write two comparison functions and call qsort() with the
first one for the first mode and with the second one for the other?
Looks to me like the most natural way.

Regards, Jens
 
M

Malcolm

Dimitris Mandalidis said:
Is
there any way to pass the mode variable to compar() or I have to
write my own qsort() ?
You'd be crazy to write your own qsort() unless it is as a learning
exercise. However it is not possible to pass an arbitrary pointer to the
comparison function except by including it in the structure to be sorted.
The obvious way round is to write several comparison functions and call
qsort() with the appropriate one.
The other solution is to make the mode variables static and put the
comparsion function in its own file with a "setmode" function. This is not
thread safe but doesn't have any of the namespace disadvantages of using
global variables.
 
B

Ben Pfaff

Dimitris Mandalidis said:
And we want to sort bar in relation to a mode, for example if mode == 1 then
sort bar by comparing a member, if mode == 2 then sort bar by comparing b
members and so on. The mode variable cannot be used as extern and qsort()
prototype needs a function compar() which takes two void * arguments. Is
there any way to pass the mode variable to compar() or I have to write my
own qsort() ?

In the general case where you want to pass some auxiliary data to
a qsort() comparison function, you're probably best off writing a
qsort() variant that allows passing auxiliary data. In my own
programs I sometimes use such a function, which I built by taking
the qsort() implementation from GNU libc and adding an auxiliary
data argument.

In this specific case where there are only a few possibilities,
just use multiple comparison functions, e.g.
qsort (array, sizeof array / sizeof *array, sizeof *array,
mode == 1 ? func_1 : (mode == 2 ? func_2 : func_3));
 
K

Kushal Kumaran

In article Ben Pfaff said:
In the general case where you want to pass some auxiliary data to
a qsort() comparison function, you're probably best off writing a
qsort() variant that allows passing auxiliary data. In my own
programs I sometimes use such a function, which I built by taking
the qsort() implementation from GNU libc and adding an auxiliary
data argument.

In this specific case where there are only a few possibilities,
just use multiple comparison functions, e.g.
qsort (array, sizeof array / sizeof *array, sizeof *array,
mode == 1 ? func_1 : (mode == 2 ? func_2 : func_3));
<delurks>
Wouldn't an array of function pointers, indexed into with mode, be nicer?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

How to apply qsort() with a parameter? 17
qsort returning index 2
qsort needs one more argument 8
Qsort-ing structures 16
qsort semantics 10
qsort + structure problem 7
qsort of malloc'ed structs 5
Command Line Arguments 0

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top