Questions from Beginners MQL5 MT5 MetaTrader 5 - page 732

 
Vitalie Postolache:
What if after sorting you change the indexing order using ArraySetAsSeries?
ArraySetAsSeries does not apply to multidimensional arrays.
 
Alexey Viktorov:
ArraySetAsSeries does not apply to multidimensional arrays.

Yes, it does not apply. And who knows what applies, please answer.

 
Vitaly Muzichenko:

Yes, it does not apply. And who knows what applies, please answer.

Sometimes I use bubble sorting on a two dimensional array. You can choose the direction and dimension by which to sort
 
Artyom Trishkin:
Sometimes I use bubble sorting on a two-dimensional array. You can choose the direction and dimension by which to sort

Come to your house for the code, or post it, if you don't mind)

Added: How resource-consuming is it?, might as well invert the loop, which I don't want to do.

 
Vitaly Muzichenko:
Come to your house for the code, or post it if you don't mind)
Well, take a beer and come on a visit ;)

I just do not remember what dimension of the array you wrote - maybe you do not need it, and I'll shove ...

//+------------------------------------------------------------------+
//| Пузырьковая сортировка двумерного массива                        |
//+------------------------------------------------------------------+
template<typename T>
void ArraySortBubbleTwoDims(T& array[][TWO_DIM], int sort_dimension=0, int sort_direction=0) {
   T     t=0;
   int   k=ArrayRange(array,1);    // Количество колонок
   int   n=ArrayRange(array,0);    // Количество строк
  
   //---
   if(sort_dimension<0) sort_dimension=0;
   if(sort_dimension>k) sort_dimension=k;
   //---
   for(int i=n-1; i>0; i--) {
      for(int j=0; j<i; j++) {
         //--- по возрастанию
         if(sort_direction==0) {
            if(array[j][sort_dimension]>array[j+1][sort_dimension]) {
               for(int e=0; e<k; e++) {
                  t=array[j][e];
                  array[j][e]=array[j+1][e];
                  array[j+1][e]=t;
                  }
               }
            }
         //--- по убыванию
         else {
            if(array[j][sort_dimension]<array[j+1][sort_dimension]) {
               for(int e=0; e<k; e++) {
                  t=array[j][e];
                  array[j][e]=array[j+1][e];
                  array[j+1][e]=t;
                  }
               }
            }
         }
      }
}
//+------------------------------------------------------------------+
I pulled it out of the stash, and quickly switched to a template. I may have made a mistake in haste...
 
Artyom Trishkin:
Well, grab a beer and come visit ;)

I just do not remember what dimension of the array you wrote - maybe you don't need it, and I'll put it here ...

It's winter, but the skis won't go

void Func()
{
double m[][3];

if(условия)
  {
   // много кода
         c++;
         ArrayResize(m, c);
         m[c-1][0]= Lots();
         m[c-1][1]= Ticket();
         m[c-1][2]= Profit();
  }
BySort(m); // передаём в функцию "BySort"
}

void BySort(double &mas[][3])
{
// Сортируем по размеру лота от большего к меньшему
  ArraySort(mas);
  ArraySetAsSeries(mas,true); // при такой записи mql5 ругается, в mql4 работает

... здесь работа с уже сортированным массивом и основной код
}

Error: "'m' - parameter conversion not allowed e.mq5 2076 20"

 
Vitaly Muzichenko:

It's winter, but the skis won't go

void Func()
{
double m[][3];

if(условия)
  {
   // много кода
         c++;
         ArrayResize(m, c);
         m[c-1][0]= Lots();
         m[c-1][1]= Ticket();
         m[c-1][2]= Profit();
  }
BySort(m); // передаём в функцию "BySort"
}

void BySort(double &mas[][3])
{
// Сортируем по размеру лота от большего к меньшему
  ArraySort(mas);
  ArraySetAsSeries(mas,true); // при такой записи mql5 ругается, в mql4 работает

... здесь работа с уже сортированным массивом и основной код
}

Error: "'m' - parameter conversion not allowed e.mq5 2076 20"

It's written in the help:

Note

The AS_SERIES flag cannot be set for multidimensional arrays and for static arrays (i.e., arrays whose size in square brackets is already specified at compile time).

And what prevents you from sorting it with the function I suggest?

 
Artyom Trishkin:
It's written in the help:

Note

The AS_SERIES flag cannot be set for multidimensional arrays and static arrays (i.e. arrays whose size in square brackets is specified at compile time).

But what prevents you from sorting it out using the function I suggest?

Exactly the same error when passing it into the function
 
Vitaly Muzichenko:
Exactly the same error when transferring to a function
Which one?
 
Artyom Trishkin:
Which one?

To your"ArraySortBubbleTwoDims".

Anyway, I've expanded the loop and the issue is solved. But I'd still like to see a normal solution in the form of a standard function

Reason: