sort the array of struct based on the data members ??

S

Santosh Nayak

Hi,
Is it possible to sort the array of struct based on the data members.
e.g.

struct {
int a ;
float b ;
char c ;

} TEMP ;

int main() {
struct TEMP array[10] ;

/* sort () ; */

}

Based on struct members, i want to sort the array[10], with out
rewriting the sort function for each member.

Any comments ??!!
 
I

Ian Collins

Santosh said:
Hi,
Is it possible to sort the array of struct based on the data members.
e.g.

struct {
int a ;
float b ;
char c ;

} TEMP ;

int main() {
struct TEMP array[10] ;

/* sort () ; */

}

Based on struct members, i want to sort the array[10], with out
rewriting the sort function for each member.
qsort?
 
S

santosh

Santosh said:
Hi,
Is it possible to sort the array of struct based on the data members.
e.g.

struct {
int a ;
float b ;
char c ;

} TEMP ;

int main() {
struct TEMP array[10] ;

/* sort () ; */

}

Based on struct members, i want to sort the array[10], with out
rewriting the sort function for each member.

Any comments ??!!

Use qsort with a comparison function, (which you'll have to supply
yourself.) Then if the sort criteria changes, all you'll have to do is
modify your comparison function. You don't need to change the actual
sorting function, (qsort), unless you actually want to use another
sorting method.
 

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

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top