Array Sort ((String Values)) MQL5

 

 >>> can you help me please ?

I can not find the equivalent in MQL5 doing sort for ((( string values ))) .... this my code in MQL4 and ArraySort works OK in string values

string x[];
ArrayResize(x,OrdersTotal()+1);
for(int i=0;i<OrdersTotal();i++)
      {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
         {
            x[i]=OrderSymbol();
         }
      }
ArraySort(x,WHOLE_ARRAY,0,MODE_ASCEND); // I can not find the equivalent in MQL5 work on string values
 
Egyptian.Smart:

 >>> can you help me please ?

I can not find the equivalent in MQL5 doing sort for ((( string values ))) .... this my code in MQL4 and ArraySort works OK in string values

https://www.mql5.com/en/docs/array/arraysort

It can not sort based on string according to the docs. 

Maybe this https://www.mql5.com/en/articles/1334 can help you to give Arrays more functionality found in other programming languages.

Documentation on MQL5: Array Functions / ArraySort
Documentation on MQL5: Array Functions / ArraySort
  • www.mql5.com
"The indicator analyzes data for the last month and draws all candlesticks with small" "to define such candlesticks. The candlesticks having the volumes comprising the first InpSmallVolume" "per cent of the array are considered small. The candlesticks having the tick volumes comprising ...
 
Egyptian.Smart:

 >>> can you help me please ?

I can not find the equivalent in MQL5 doing sort for ((( string values ))) .... this my code in MQL4 and ArraySort works OK in string values


I solve My Problem by working with ArrayString.mqh file

#include <Arrays\ArrayString.mqh> // put this line at the begin of code after #property lines

void OnTick()
{
      CArrayString *array=new CArrayString; // put this line at the begin of OnTick() or at the begin of OnStart()
.
.
.
      string x[];
      for(int i=0;i<PositionsTotal();i++)
      {
         PositionGetTicket(i);
         array.Insert(PositionGetString(POSITION_SYMBOL),i);
      }
      array.Sort(0);
      for(int i=0;i<PositionsTotal();i++)
      {
         x[i]=array.At(i);
      }
 
Egyptian.Smart:

I solve My Problem by working with ArrayString.mqh file


Glad you solved your problem and posted the solution. Thanks!

 
Enrique Dangeroux:

https://www.mql5.com/en/docs/array/arraysort

It can not sort based on string according to the docs. 

Maybe this https://www.mql5.com/en/articles/1334 can help you to give Arrays more functionality found in other programming languages.


Thanks for your response ... I found the solution ... and i put it in comments

 
Egyptian.Smart:

I solve My Problem by working with ArrayString.mqh file


Hi, just a couple of tips when working with the standard library collections... See comments in code.

void OnStart()
{
   //CArrayString *array=new CArrayString; // put this line at the begin of OnTick() or at the begin of OnStart()
   CArrayString array; //Better to statically instantiate so you don't have to remember to delete. 
   string x[]; //You already have a string array object... no point in having two... but leaving it here for demonstration
   for(int i=0;i<PositionsTotal();i++)
   {
      PositionGetTicket(i);
      //array.Insert(PositionGetString(POSITION_SYMBOL),i);
      //Add is safer and quicker. Only use insert when you have to specify the index
      array.Add(PositionGetString(POSITION_SYMBOL));    
   }
   //array.Sort(0);
   array.Sort();
   ArrayResize(x,array.Total());
   //for(int i=0;i<PositionsTotal();i++)
   for(int i=0;i<array.Total();i++)
   {
      //x[i]=array.At(i);

// use the overloaded operator instead

      x[i]=array[i];    } }
 
Egyptian.Smart:

I solve My Problem by working with ArrayString.mqh file

There are a lot of ways to code inefficiently. If you are just happy it's working no problem for me, but putting all these strings in an array to sort it, is really time consuming.

May I ask you why you need to sort symbols used on every position ?

 
Ahmed Alesawy:

I solve My Problem by working with ArrayString.mqh file

Thanks, this helped me a ton!

 
nicholi shen:

Hi, just a couple of tips when working with the standard library collections... See comments in code.

These tips worked like gold for me, thank you!