I ve got a 2-D array that looks as such
23 hey
34 are
29 how
38 you
26 there
How would I sort the first column of integers while also bringing over the strings so that I get
23 hey
26 there
29 how
34 are
28 you
Thanks
Printable View
I ve got a 2-D array that looks as such
23 hey
34 are
29 how
38 you
26 there
How would I sort the first column of integers while also bringing over the strings so that I get
23 hey
26 there
29 how
34 are
28 you
Thanks
It looks like a 1D array to me. Perhaps you mean a parrallel array?
Sort the array of integers. Everytime you copy something from n to m, copy the corresponding parrallel array index from n to m.
Better yet, use a struct with an int and a string, and just sort based on the integer part.
I've sorted them using string compare but that is sorting by string while I'm wanting a sort by integer. Can you elaborate on your response. Thanks
I think he means something like this:Quote:
Originally posted by Unregistered
I've sorted them using string compare but that is sorting by string while I'm wanting a sort by integer. Can you elaborate on your response. Thanks
You can use various sorting algorithms for that but essentially the idea is the same. You can however create two seperate arrays but, again it would be done the same way. Using a struct or even a class just simplifies everything. You shouldn't be able to make it a 2 dimensional array since 1 side is an integer and the other side is a string.Code:struct blah {
int number;
string stuff;
}
sorting algorithm(blah a[])
{
for(int i = 0 ; i < range-1; i++) {
....
....
....
//assuming a[i+1].number < a[i].number
tempstring = a[i+1].stuff;
tempnumber = a[i+1].number;
a[i+1].stuff = a[i].stuff;
a[i+1].number = a[i].number;
a[i].stuff = tempstring;
a[i].number = tempnumber;
}
}