Sorting Multiple Arrays Together in C++

Discussion in 'C++' started by jpoloney@gmail.com, Mar 27, 2006.

  1. Guest

    I was wondering if there was a quick and easy way to sort multiple
    arrays in C++. What I mean is that, say I have 3 integer arrays. They
    are in order by array indices (array1[0] corresponds to array2[0] and
    array3[0], etc). I want to do something exactly like the Excel sort of
    "Sort by A, then by B, then by C". Meaning, sort array A in ascending
    order, then inside of that sort B in ascending order, etc. Here is some
    pseudo code for what I'm trying to do:

    array1 = [1, 1, 2, 2, 3, 3];
    array2 = [2, 1, 2, 1, 2, 1];
    array3 = [1, 2, 3, 4, 5, 6];

    sort_1_then_2_then_3();

    array1 = [1, 1, 2, 2, 3, 3];
    array2 = [1, 2, 1, 2, 1, 2];
    array3 = [2, 1, 4, 3, 6, 5];

    Is there anyway to do this using STL sorts in C++ or would I have to
    write this algorithm from scratch?
    , Mar 27, 2006
    #1
    1. Advertising

  2. Daniel T. Guest

    In article <>,
    wrote:

    > I was wondering if there was a quick and easy way to sort multiple
    > arrays in C++. What I mean is that, say I have 3 integer arrays. They
    > are in order by array indices (array1[0] corresponds to array2[0] and
    > array3[0], etc). I want to do something exactly like the Excel sort of
    > "Sort by A, then by B, then by C". Meaning, sort array A in ascending
    > order, then inside of that sort B in ascending order, etc. Here is some
    > pseudo code for what I'm trying to do:
    >
    > array1 = [1, 1, 2, 2, 3, 3];
    > array2 = [2, 1, 2, 1, 2, 1];
    > array3 = [1, 2, 3, 4, 5, 6];
    >
    > sort_1_then_2_then_3();
    >
    > array1 = [1, 1, 2, 2, 3, 3];
    > array2 = [1, 2, 1, 2, 1, 2];
    > array3 = [2, 1, 4, 3, 6, 5];
    >
    > Is there anyway to do this using STL sorts in C++ or would I have to
    > write this algorithm from scratch?


    If you can rearrange your data so that you have one array that contains
    objects that have three elements each, then you can use the standard
    library functions to do the job using lexicographical_compare and sort.

    struct Type {
    int rep[3];
    };

    friend bool operator<( const Type& lhs, const Type& rhs ) {
    return lexicographical_compare( lhs.rep, lhs.rep + 3,
    rhs.rep, rhs.rep + 3 );
    }

    Type array[6];
    (load the array up with the data.)

    sort( array, array + 6 );


    --
    Magic depends on tradition and belief. It does not welcome observation,
    nor does it profit by experiment. On the other hand, science is based
    on experience; it is open to correction by observation and experiment.
    Daniel T., Mar 28, 2006
    #2
    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. =?Utf-8?B?TW9yaQ==?=
    Replies:
    3
    Views:
    314
    Patrick.O.Ige
    Jan 18, 2006
  2. agent349
    Replies:
    12
    Views:
    469
    Mark A. Gibbs
    Apr 18, 2004
  3. Philipp
    Replies:
    21
    Views:
    1,111
    Philipp
    Jan 20, 2009
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    467
    Michael Budash
    Oct 11, 2003
  5. René Scheibe

    sorting multiple arrays

    René Scheibe, Nov 11, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    133
    Andrew Tkachenko
    Nov 11, 2004
Loading...

Share This Page