Thread: ordering arrays
View Single Post
Join Date: Aug 2005
Posts: 15,152
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: ordering arrays

 
0
  #3
Nov 20th, 2008
You want to sort the error array and keep the relationship with the rows in weights array ???

One way is to use another array that contains indices into both error and weights array. When you sort error array actually move the rows in this third array. Use the third array as indirect access to the other arrays.
  1. indices[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  2. // sort errors array
  3. for(int i = 0; i < 15; i++)
  4. {
  5. for(int j= i+1; j < 16; j++)
  6. {
  7. if( errors[ indices[i] ] < errors[ indices[j] ] )
  8. {
  9. int temp = indices[i];
  10. indices[i] = indices[j];
  11. indices[j] = temp;
  12. }
  13. }
  14. }

Now to reference weights: assume variable X is an integer that contains the value from errors index
weights[ indices[x] ][0];

Or, if that is a little too complex for you, just sort both errors and weights array at the same time. When you exchange elements of errors array also exchange all columns in the corresponding rows of weights.
Last edited by Ancient Dragon; Nov 20th, 2008 at 9:04 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote