Sorting Multidimensional Array

Discussion in 'C++' started by Index, May 27, 2006.

  1. Index

    Index Guest

    Hi, I have a 4 dimensional array and I want to sort it by the first two
    columns emulating an ORDER BY clause to some extent.Any suggestion?

    The original Array is:

    Row1-> 4 8 2 9
    Row2-> 4 5 5 3
    Row3-> 3 9 8 7
    Row4-> 8 2 4 8

    The sorted array should look:

    Row1-> 3 9 8 7
    Row2-> 4 5 5 3
    Row3-> 4 8 2 9
    Row4-> 8 2 4 8

    In fact the columns upon which the sorting is to be done is also
    dynamic.
    Please provide me some efficient algo or implementation.The data to be
    handled is rather huge.So efficiency is a vital requirement.
    Thanks.
    Index, May 27, 2006
    #1
    1. Advertising

  2. Index

    Ian Collins Guest

    Index wrote:
    > Hi, I have a 4 dimensional array and I want to sort it by the first two
    > columns emulating an ORDER BY clause to some extent.Any suggestion?
    >
    > The original Array is:
    >
    > Row1-> 4 8 2 9
    > Row2-> 4 5 5 3
    > Row3-> 3 9 8 7
    > Row4-> 8 2 4 8
    >
    > The sorted array should look:
    >
    > Row1-> 3 9 8 7
    > Row2-> 4 5 5 3
    > Row3-> 4 8 2 9
    > Row4-> 8 2 4 8
    >
    > In fact the columns upon which the sorting is to be done is also
    > dynamic.
    > Please provide me some efficient algo or implementation.The data to be
    > handled is rather huge.So efficiency is a vital requirement.


    Store your data in a struct and define a < operator for the struct based
    on your sort rules. Then shove the data structs into a set.

    Something like:

    struct Data
    {
    int a, b, c, d, e;

    bool operator<( const Data& ) const;
    };

    bool Data::eek:perator<( const Data& data ) const
    {
    return (a < data.a) ? true : (a == data.a) ? data.b > b : false;
    }

    If you want greater efficiency, create a smart pointer type for Data
    with its own < operator and put these in the set.

    --
    Ian Collins.
    Ian Collins, May 27, 2006
    #2
    1. Advertising

  3. Index

    Ian Collins Guest

    Ian Collins wrote:
    >
    > If you want greater efficiency, create a smart pointer type for Data
    > with its own < operator and put these in the set.
    >

    I should have added - be sure to profile both options and don't bother
    with the second unless you realy have to!

    --
    Ian Collins.
    Ian Collins, May 27, 2006
    #3
  4. On 26 May 2006 17:45:46 -0700, "Index" <>
    wrote:
    >Hi, I have a 4 dimensional array and I want to sort it by the first two
    >columns emulating an ORDER BY clause to some extent.Any suggestion?
    >
    >The original Array is:
    >
    >Row1-> 4 8 2 9
    >Row2-> 4 5 5 3
    >Row3-> 3 9 8 7
    >Row4-> 8 2 4 8
    >
    >The sorted array should look:
    >
    >Row1-> 3 9 8 7
    >Row2-> 4 5 5 3
    >Row3-> 4 8 2 9
    >Row4-> 8 2 4 8
    >
    >In fact the columns upon which the sorting is to be done is also
    >dynamic.
    >Please provide me some efficient algo or implementation.The data to be
    >handled is rather huge.So efficiency is a vital requirement.
    >Thanks.


    It's not possible to use qsort or std::sort directly because arrays
    are not first class objects in C/C++. You should use a struct wrapper
    for your Rows as vaguely indicated by Ian Collins.

    Best wishes,
    Roland Pibinger
    Roland Pibinger, May 27, 2006
    #4
  5. Index

    Ian Collins Guest

    Roland Pibinger wrote:
    >
    > It's not possible to use qsort or std::sort directly because arrays
    > are not first class objects in C/C++. You should use a struct wrapper
    > for your Rows as vaguely indicated by Ian Collins.
    >

    vaguely?

    "Store your data in a struct and define a < operator for the struct
    based on your sort rules."

    I guess I could have shouted :)

    --
    Ian Collins.
    Ian Collins, May 27, 2006
    #5
  6. Index

    Phlip Guest

    Ian Collins wrote:

    > Roland Pibinger wrote:
    >>
    >> It's not possible to use qsort or std::sort directly because arrays
    >> are not first class objects in C/C++. You should use a struct wrapper
    >> for your Rows as vaguely indicated by Ian Collins.
    >>

    > vaguely?


    Arrays are not first class objects?

    template<class T, const size_t extent>
    size_t
    extent_of(T(&)[extent]) { return extent; }

    RP meant that arrays are tricky to use as first class objects, and that
    those sorters require a wrapper...

    --
    Phlip
    http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
    Phlip, May 27, 2006
    #6
  7. On Sat, 27 May 2006 21:59:35 GMT, "Phlip" <> wrote:
    >Arrays are not first class objects?
    >
    > template<class T, const size_t extent>
    > size_t extent_of(T(&)[extent]) { return extent; }


    I don't know what you want to demonstrate. The original problem,
    sorting a 2-dimensional array (preferably with Standard facilities
    like sort or qsort), is still unresolved in the currend thread.

    Best regards,
    Roland Pibinger
    Roland Pibinger, May 28, 2006
    #7
    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. Dave Bazell

    slice of multidimensional array

    Dave Bazell, Jul 23, 2003, in forum: Perl
    Replies:
    2
    Views:
    4,057
  2. epigram
    Replies:
    1
    Views:
    10,768
    =?Utf-8?B?c29jaWV0b3BpYS5uZXQ=?=
    Jul 16, 2005
  3. Ben
    Replies:
    8
    Views:
    12,546
    Eki Y. Baskoro
    Dec 18, 2003
  4. Gaspard Kayitare

    Sorting multidimensional arrays

    Gaspard Kayitare, Apr 17, 2005, in forum: C++
    Replies:
    1
    Views:
    545
    Allan Bruce
    Apr 17, 2005
  5. Rehceb Rotkiv
    Replies:
    16
    Views:
    867
    Alex Martelli
    Apr 2, 2007
Loading...

Share This Page