how to sort and display in color

 

Say I have follow data

Nr.    B/S     Pair       Profit

1 Buy   EURUSD  -90.12

2.     Sell   USDJPY   100.04

3.     Sell  AUDUSD  -100.12

4.    Buy   GBPUSD  -123.34

5.    Sell   CADJPY  198.34


How do I use array and sort them by profit in descending order and display positive profit in green and negative in red as below:

Nr.    B/S       Pair       Profit

1.      Sell    CADJPY   198.34

2.     Sell     USDJPY   100.04

3.     Buy     EURUSD  -90.12

4.     Sell     AUDUSD  -100.12

5.     Buy     GBPUSD  -123.34


Thanks in advance.

 
   double indexmap[][2];
   int rows=YourNumberOfDataRows;
   ArrayResize(indexmap,rows);

   for(int i=0; i<rows; i++) {
      indexmap[i][0]=ProfitOfRow(i);
      indexmap[i][1]=i;
   }
   //--- sorting of indexmap[][] by first dimension (descending) 
   ArraySort(indexmap,WHOLE_ARRAY,0,MODE_DESCEND); 
   //--- indexmap is now sorted by profit, descending, together with index, like so: [198.34, 5], [100.04, 2], ...

   for(int j=0; j<rows; j++) {
      double profit=indexmap[j][0];
      int i=(int)indexmap[j][1];
      color c=(profit<0 ? clrRed : clrGreen);
      PrintDataRow(i,c); // print row i of the data, using color c
   }
See also ArraySort in MQL4 reference for a sample and more information. https://docs.mql4.com/array/arraysort
ArraySort - Array Functions - MQL4 Reference
ArraySort - Array Functions - MQL4 Reference
  • docs.mql4.com
//| Script program start function                                    |
 

Thank you very much.

I tried your code and MT4 took a long time to load or not even loaded. 


BY the way I have Pair[] and Profit[] to hold pair and profit values.

 
XIn Li:

Thank you very much.

I tried your code and MT4 took a long time to load or not even loaded. 

Show all of your code here if it does not appear to work correctly for you.
Reason: