Sort array of struct based on the data members.

Discussion in 'C++' started by Santosh Nayak, Mar 14, 2007.

  1. 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 ??!!
    Santosh Nayak, Mar 14, 2007
    #1
    1. Advertising

  2. Santosh Nayak

    Ian Collins Guest

    Santosh Nayak wrote:
    > Hi,


    Please don't multi-post on Usenet.

    --
    Ian Collins.
    Ian Collins, Mar 14, 2007
    #2
    1. Advertising

  3. Santosh Nayak

    Mike Wahler Guest

    "Santosh Nayak" <> wrote in message
    news:...
    > 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
    Mike Wahler, Mar 14, 2007
    #3
  4. Santosh Nayak

    Alan Cui Guest

    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 ??!!
    Alan Cui, Mar 14, 2007
    #4
  5. Santosh Nayak

    Piyo Guest

    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<myStruct> m;
    // fill m with elements
    std::sort( m.begin(), m.end(), ComparisonOp() );
    }
    Piyo, Mar 14, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Angus Comber
    Replies:
    7
    Views:
    1,142
    Richard Heathfield
    Feb 5, 2004
  2. Chris Fogelklou
    Replies:
    36
    Views:
    1,345
    Chris Fogelklou
    Apr 20, 2004
  3. Santosh Nayak
    Replies:
    2
    Views:
    496
    santosh
    Mar 14, 2007
  4. Tuan  Bui
    Replies:
    14
    Views:
    461
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  5. John Reye
    Replies:
    28
    Views:
    1,332
    Tim Rentsch
    May 8, 2012
Loading...

Share This Page