3 Replies - 1584 Views - Last Post: 19 April 2011 - 06:58 AM Rate Topic: -----

#1 teopp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 06-March 11

Sort two arrays at the same time

Posted 18 April 2011 - 11:47 AM

Hello there,

I'm kinda in need for your help on this one.

The situation is the following:

I have two different arrays. I want to sort one of them, but I need the other one to sort exactly the same way as the first one.

Example: If on my first array the new position number 1 is the old position number 50, I want the same to happen with another array.

I could use a struct to do this, but this is not very efficient for what I want. It's not efficient because I need one of the arrays in the beggining of the code, and it will allocate memory. But, I only need the other array later on the code, so it won't make sense to allocate space for it if I'm only going to need it later.

Hope you got what I need.

Thanks for your help.

This post has been edited by teopp: 18 April 2011 - 11:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Sort two arrays at the same time

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Sort two arrays at the same time

Posted 19 April 2011 - 04:30 AM

Just duplicate the moves you make in the first array in the second array.

Vaguely like this
if(firstArray[i] > firstArray[j]){
   temp = firstArray[i];
   firstArray[i] = firstArray[j];
   firstArray[j] = temp;

   temp = secondArray[i];
   secondArray[i] = secondArray[j];
   secondArray[j] = temp;
}



It seems simple as you set out the question.
Is there something that you are not telling us that makes it harder than it seems?

This post has been edited by janotte: 19 April 2011 - 04:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 teopp  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 06-March 11

Re: Sort two arrays at the same time

Posted 19 April 2011 - 06:18 AM

Well I didn't mention that the data in two arrays isn't the same. I just want their positions to change, not to equal the content of them both.

Just an example:

Array 1 has names:

Jack
John
Morgan
Peter
(...)

Array 2 has their age, for instance:

14
56
45
23
(...)

Now I want to sort it from the youngest to the oldest person. To do that I must sort the Array 2, anyway I also need the array 1 to change because their positions are related.

Hope you got what I meant to say :P

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Sort two arrays at the same time

Posted 19 April 2011 - 06:58 AM

The suggestion that janotte gave you does exactly that, have you tried it?

View Postteopp, on 18 April 2011 - 07:47 PM, said:

I could use a struct to do this, but this is not very efficient for what I want. It's not efficient because I need one of the arrays in the beggining of the code, and it will allocate memory. But, I only need the other array later on the code, so it won't make sense to allocate space for it if I'm only going to need it later.
What makes you think that its not very efficient? If you need to allocate memory during the lifetime of your program, and you know for certain that you are going to use it, then you don't gain anything by waiting to allocate it.

In fact, usually reserving memory before you need it is actually more efficient than allocating twice. it just so happens that using a struct in this case will make your life much easier too.

This post has been edited by Bench: 19 April 2011 - 06:59 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1