473,769 Members | 1,674 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 2791
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
5318
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
4325
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
3531
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
25173
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
2712
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
3222
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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
10045
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
9994
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
6673
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();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.