473,746 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sort a struct of arrays

i've been looking through the topics here and i can't quite find one
that helps me. i'm trying to sort a struct that contains three arrays
using the sort() function. the three arrays are parallel and need to be
grouped together when it is sorted. for instance, if i sort the int
array by ascending order, i need the name and grade associated with
that int array to also move with it. i hope what i said is not too
confusing. what am i doing wrong? here is the code i'm using:

struct courses {
string student[7];
string idNumber[7];
string score[7];

} course1, course2, temp;

course1.student[0] = "Name";
course1.student[1] = "Name";
....
course1.student[6] = "Name";

course1.idNumbe r[0] = "001221";
course1.idNumbe r[1] = "001543";
....
course1.idNumbe r[6] = "001334";

course1.score[0] = "38";
course1.score[1] = "98";
....
course1.score[6] = "67";

sort(course1.id Number, course1.idNumbe r+7)
// end

i realize that sorting like what i have above only sorts that one
array. but since i want all the arrays to be linked to each other, i
need to move the other two arrays to the same position when it sorts
the idNumber. is there a different way to do this? the only stipulation
is that i need to have three parallel arrays for each course1 and
course2.

Oct 28 '05 #1
3 2789
cp******@gmail. com wrote:
i've been looking through the topics here and i can't quite find one
that helps me. i'm trying to sort a struct that contains three arrays
using the sort() function. the three arrays are parallel and need to be
grouped together when it is sorted. for instance, if i sort the int
array by ascending order, i need the name and grade associated with
that int array to also move with it. i hope what i said is not too
confusing. what am i doing wrong? here is the code i'm using:

struct courses {
string student[7];
string idNumber[7];
string score[7];

} course1, course2, temp;

course1.student[0] = "Name";
course1.student[1] = "Name";
...
course1.student[6] = "Name";

course1.idNumbe r[0] = "001221";
course1.idNumbe r[1] = "001543";
...
course1.idNumbe r[6] = "001334";

course1.score[0] = "38";
course1.score[1] = "98";
...
course1.score[6] = "67";

sort(course1.id Number, course1.idNumbe r+7)
// end

i realize that sorting like what i have above only sorts that one
array. but since i want all the arrays to be linked to each other, i
need to move the other two arrays to the same position when it sorts
the idNumber. is there a different way to do this? the only stipulation
is that i need to have three parallel arrays for each course1 and
course2.

Why don't you create an array of 'course' objects:

struct course {
string student;
string idNumber;
string score;
};

struct SortCourseById {
bool operator<(const course& a, const course &b) const { return a.idNumber < b.idNumber; }
};

course courses[7];

sort(courses, course+7, SortCourseById) ;
Or am I missing something ?
Stefan
--
Stefan Naewe
naewe.s_AT_atla s_DOT_de
Oct 28 '05 #2
Stefan Näwe wrote:
struct course {
string student;
string idNumber;
string score;
};

struct SortCourseById {
bool operator<(const course& a, const course &b) const { return
a.idNumber < b.idNumber; }
ITYM:

bool operator()(cons t course& a, const course &b) const
{
return a.idNumber < b.idNumber;
}
};


Oct 28 '05 #3
Rolf Magnus wrote:
Stefan Näwe wrote:

struct course {
string student;
string idNumber;
string score;
};

struct SortCourseById {
bool operator<(const course& a, const course &b) const { return
a.idNumber < b.idNumber; }

ITYM:

bool operator()(cons t course& a, const course &b) const
{
return a.idNumber < b.idNumber;
}

};


Ooops...
Sorry.
Thanks.

Stefan
--
Stefan Naewe
naewe.s_AT_atla s_DOT_de
Oct 28 '05 #4

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

Similar topics

4
5317
by: Brett | last post by:
I have two arrays and i wish to sort the first one numerically, but after sorting, I would like the second array to be in the same matching order as the first array. ie. @l1={3,1,2}; @l2={'a','b','c'}; @l1 = sort {$a <=> $b} (@l1); # sort list @l1 to be {1,2,3}
40
4316
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the number of records. Both these numbers are known
6
3530
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within the data about the same person: Original Data: id month sales +-----+ +-----+ +-----+ | 432 | | 8 | | 89 | +-----+ +-----+ +-----+ | 123 | | 8 | | 116 |
7
25171
by: ritchie | last post by:
Hi all, I am new to this group and I have question that you may be able to help me with. I am trying to learn C but am currently stuck on this. First of all, I have a function for each sort (Bubble, insertion, selection..). I have an array of int's and am passing them to each sort function.
3
2710
by: chellappa | last post by:
hi this simple sorting , but it not running...please correect error for sorting using pointer or linked list sorting , i did value sorting in linkedlist please correct error #include<stdio.h> #include<stdlib.h> int main(void) {
21
3219
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
1
2268
by: stromhau | last post by:
Hi, I have made a few classes in c++. They somehow cooperate doing some 3d stuff. Basically it is a moving camera acting as a flight, i have placed a lot of objects around the scene together with a b-spline surfcae. The problem class is the polygon class. this class read 3d files generates faces, edges and so on. here are two of the building blocks(structs) struct FACE{
4
5069
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or deleted, however it must be initialized by a function during run-time to contain so many users which each contain so many directories of which each contain so many files. I've completed the program and have it running flawlessly without implementing...
10
6189
by: John | last post by:
I have two separate arrays A and B. The comparison function only depends on elements in A (keys are in A), but the swap() function needs to swap not only A,A, but also B,B ; whenever it swaps A,A. Can this be done using internal sort() of C++? If not, what is the easiest way to accomplish this. Speed is of paramount importance...copying/merging the arrays is out of question... Thanks a lot for your help.
0
8800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9500
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9218
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8227
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6772
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6060
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.