By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 ??!!

Mar 14 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Santosh Nayak wrote:
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?

--
Ian Collins.
Mar 14 '07 #2

P: n/a
Santosh Nayak wrote:
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.

Mar 14 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.