473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Sorting Multiple Arrays Together in C++

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 '06 #1
1 6268
In article <11*********************@i40g2000cwc.googlegroups. com>,
jp******@gmail.com 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.
Mar 27 '06 #2

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

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
8
by: Mike MacSween | last post by:
tblCourses one to many to tblEvents. A course may have an intro workshop (a type of event), a mid course workshop, a final exam. Or any combination. Or something different in the future. At...
3
by: cs_hart | last post by:
I have the following data custID orderNum trackingNum ShipDate I have put these in multiple arrays since I want to redim. For example, the user selects one shipdate and I keep only the entries...
6
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ...
5
by: vj | last post by:
Hi all, I am using C++Builder-5. I want to run multiple cpp files at the same time. If I use the C++Builder for running a cpp file (i.e., I just double click the cpp file, it then opens in the...
13
by: JoeC | last post by:
I am completly lost. I would like to create a function that takes two vectors. These two vectors have objects with x and y coords, what I want to do find all the objects in the same x and y...
2
by: zfareed | last post by:
I managed to sort a 2-dim array of lastnames but I am having trouble with printing the corresponding first names which are stored in a different 2-dim array. Here is my code for: ...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.