How to sort an array of string values in MQL

>>> 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

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.

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);
      }

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

Hi, just a couple of tips when working with the standard library. 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];
   }
}

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 ?