Sorting Arrays

 
I am use to coding in AS3. Is there an equivalent of ".pop", ".push", ".name", or "(substr)" functionality for array items? i am trying to create two arrays. First an array of closing prices, and the second an array of int variables which is the index numbers of the in the first array. Then I want to sort the first array by values (largest to smallest)... And resort the values in the second array to move to the same indexes in the second array, as the correspond items in the first array moved to. For exam double arr1[] = {a,b,c,d,e,f} and arr 2[] = {1,2,3,4,5,6}. When I resort arr1[] to {c,f,b,a,d,e} the. I want arr2[] = {3,6,2,1,4,5}. Any suggestions? Thanks
 
  1. Off course not. you have to do it yourself.
  2. You could write a sort secondary array but since you said they are an array of Closing prices, just sort the indexes
Not compiled, not tested
#define ASCENDING  +1
#define DESCENDING -1
int      GetShiftsOrderByClose(int& shifts[], int iLimit, int iCurr = 0, 
                                                         double asc=ASCENDING){
   int nShifts = iLimit - iCurr;
   if(ArraySize(shifts) <= nShifts)  ArrayResize(shifts, nShifts);
   for(nShifts = 0; iCurr < iLimit; iCurr++){
      double   curr = Close[iCurr];
      for(int iEmpty = nShifts; iEmpty > 0; iEmpty--){   // Insertion sort.
         int      iPrev = shifts[iEmpty-1];  
         double   prev = Close[iPrev];
         if((curr - prev)*asc >= 0.) break;
         shifts[iEmpty] = iPrev;
      }
      shifts[iEmpty] = iCurr;    nShifts++;       // Insert.
   }
}
Not compiled, not tested
 
WHRoeder:
  1. Off course not. you have to do it yourself.
  2. You could write a sort secondary array but since you said they are an array of Closing prices, just sort the indexes
Not compiled, not tested Not compiled, not tested

Thank you for your help. I see where you are going with this! It just seems like a lot of code to do something very simple. I just surprised that there is not more coding options for manipulating data in arrays, particularly, since most of the data used comes in an array format. Thank you again.

 
You didn't ask to sort an array, you asked to sort one array AND do something at the same time. You have to code the other something along with the sort. There are no objects (with an implied comparison) in mq4.
 
Boost:

Thank you for your help. I see where you are going with this! It just seems like a lot of code to do something very simple. I just surprised that there is not more coding options for manipulating data in arrays, particularly, since most of the data used comes in an array format. Thank you again.


Is there a way to save data to an array that is the result of a formula going through a "for loop"? So, for each loop of it, the variable "results" is saved in the array, "Array1" at the increase index?

double Array1[];

double results;

for (int i = 0; i<someNumber; i++)

{

results = iHigh[i]-iLow[i]+ iClose[i] + iOpen[i];

Array1[i] = results;

}

 
Boost:


Is there a way to save data to an array that is the result of a formula going through a "for loop"? So, for each loop of it, the variable "results" is saved in the array, "Array1" at the increase index?

<CODE REMOVED>


Please edit your post . . . please use the SRC button to post code: How to use the SRC button.

double Array1[];

Has a size of zero elements . . .

ArrayResize()

 
Boost: Is there a way to save data to an array t
  1. Use SRC
  2. Use non-empty array
  3. That is not what you originally posted.
 
WHRoeder:
  1. Use SRC
  2. Use non-empty array
  3. That is not what you originally posted.

Thank you. That worked.
Reason: