446,227 Members | 1,290 Online Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

# Sort 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 ; /* sort () ; */ } Based on struct members, i want to sort the array, with out rewriting the sort function for each member. Any comments ??!! Mar 14 '07 #1
4 Replies

 P: n/a Santosh Nayak wrote: Hi, Please don't multi-post on Usenet. -- Ian Collins. Mar 14 '07 #2

 P: n/a "Santosh Nayak" #include #include #include #include struct s { int a; float b; char c; }; bool by_a(const s& lhs, const s& rhs) { return lhs.a < rhs.a; } bool by_b(const s& lhs, const s& rhs) { return lhs.b < rhs.b; } bool by_c(const s& lhs, const s& rhs) { return lhs.c < rhs.c; } std::ostream& operator<<(std::ostream& os, const s& obj) { return os << obj.a << '\t' << obj.b << '\t' << obj.c; } void show(const s *arr, std::size_t elems, const std::string& prefix = "", std::ostream& os = std::cout) { os << prefix; std::copy(arr, arr + elems, std::ostream_iterator(os, "\n")); os << '\n'; } int main() { s array[] = { {42, 3.14, 'X'}, {25, 9.99, 'A'}, {32, 7.77, 'R'}, }; const std::size_t elems(sizeof array / sizeof *array); show(array, elems, "Unsorted:\n"); std::sort(array, array + elems, by_a); show(array, elems, "Sorted by member a:\n"); std::sort(array, array + elems, by_b); show(array, elems, "Sorted by member b:\n"); std::sort(array, array + elems, by_a); show(array, elems, "Sorted by member c:\n"); return 0; } Output: Unsorted: 42 3.14 X 25 9.99 A 32 7.77 R Sorted by member a: 25 9.99 A 32 7.77 R 42 3.14 X Sorted by member b: 42 3.14 X 32 7.77 R 25 9.99 A Sorted by member c: 25 9.99 A 32 7.77 R 42 3.14 X Now that you see what the standard library can do for you, perhaps you'll be motivated to replace that array with a std::vector. -Mike Mar 14 '07 #3

 P: n/a Define a Strict Weak Ordering function to compare two instances of this struct and then use "sort" to sort this array for u. 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 ; /* sort () ; */ } Based on struct members, i want to sort the array, with out rewriting the sort function for each member. Any comments ??!! Mar 14 '07 #4

 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 ; /* sort () ; */ } Based on struct members, i want to sort the array, with out rewriting the sort function for each member. Any comments ??!! yes you can. given std::sort(), you can pass in a Comparison functor that basically expresses the operator< meaning for your struct which naturally can be based on the data members of your struct. Here is a quick example: #include #include struct myStruct { int foo; }; class ComparisonOp : public std::binary_function { public: result_type operator()( const first_argument_type &a, const second_argument_type &b ) const { return (a.foo < b.foo); } }; int main() { std::vector

### This discussion thread is closed

Replies have been disabled for this discussion. 