472,345 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Sort array of struct based on the data members.

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
4 5100
Santosh Nayak wrote:
Hi,
Please don't multi-post on Usenet.

--
Ian Collins.
Mar 14 '07 #2

"Santosh Nayak" <sa***********@gmail.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
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::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<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
Mar 14 '07 #3
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[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 #4
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 ??!!
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<myStructm;
// fill m with elements
std::sort( m.begin(), m.end(), ComparisonOp() );
}
Mar 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JasBascom | last post by:
I thought it was too much to put this program in with my last message. The program compiles ok, but when I execute, I get access errors. can...
6
by: Eric Smith | last post by:
Is a structure containing an incomplete array as its last element (per paragraph 2 of section 6.7.2.1 of ISO/IEC 9899:1999 (E)) itself an...
16
by: Gerrit | last post by:
Hello, Is it possible to sort an array with a struct in it? example: I have a struct: public struct mp3file { public int tracknr;
5
by: sherifffruitfly | last post by:
Hi, I'm just learning cpp, and the exercise I'm working on is basically as follows: 1) Create a struct type with 4 members (char, char, char,...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef...
10
by: Roman Mashak | last post by:
Hello, All! I've met the code containing this kind of structure: typedef struct cmd { unsigned int Cmd; unsigned int Code; unsigned int...
2
by: Santosh Nayak | last post by:
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 ;
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.