Sort 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 ??!!
 
M

Mike Wahler

Santosh Nayak 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 ??!!

Yes. Use the standard library features.

#include <algorithm>
#include <cstddef>
#include <iostream>
#include <ostream>
#include <string>

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::eek:stream& operator<<(std::eek:stream& 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::eek:stream& os = std::cout)
{
os << prefix;
std::copy(arr, arr + elems, std::eek:stream_iterator<s>(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
 
A

Alan Cui

Define a Strict Weak Ordering function to compare two instances of
this struct and then use "sort" to sort this array for u.
 
P

Piyo

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 ??!!

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 <vector>
#include <functional>

struct myStruct
{
int foo;
};

class ComparisonOp :
public std::binary_function<bool, myStruct, myStruct>
{
public:
result_type operator()( const first_argument_type &a,
const second_argument_type &b ) const
{
return (a.foo < b.foo);
}
};

int
main()
{
std::vector<myStruct> m;
// fill m with elements
std::sort( m.begin(), m.end(), ComparisonOp() );
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top