Download MetaTrader 5
To add comments, please log in or register
Do you want to write a trading robot? Find solution in MQL5 Reference!
Marcos Silva
305
Marcos Silva 2015.07.18 04:49 

Hello folks, 

 I have the this sample data in my .csv file:

 

1,0.31,1.3,2.1

2,1.52,1.1,2.3

3,2.1,1.8.3.1

n+1,n,n,n

and i would like to sort the rows on a descending order by the second column keeping the row values intact and then trim the file and keep the first N rows.

 

Any insights, code snippets?

 

Thank You 

JD4
1101
JD4 2015.07.18 05:19  
Not sure if there is a way you could do it in MQL, but I am 99% sure you could do that in excel or something similar.  If you don't have excel there is a free office-stype program put out called OpenOffice.  Part of that package is a spreadsheet program.  It, like other spreadsheet programs, has the ability to import .csv files.
whroeder1
13610
whroeder1 2015.07.18 11:50  
investguy: i would like to sort the rows on a descending order by the second column keeping the row values intact and then trim the file and keep the first N rows.
  1. Read the values into a 2 dimensional array A[][4]
  2. ArraySort will sort by first element (so put your second column in A[i][0].
  3. Rewrite the first N rows to the file.
Second option, is custom sorting.
struct Data{ 
   int n; double A, B, C;
   // Sort by second column, ascending.
   bool is_before(const Data& a, const Data& b) const{return a.A < b.A;}
};
Data array[]; 
loop{ resize array; 
      file read array[i].n, array[i].A, ...
}
insertion_sort(array[0], array, nArray)
Compiled but not tested.
#define INDEX uint
template <typename Datatype, typename BinaryPredicate>
void     insertion_sort(
                  const BinaryPredicate&  comp,    ///<[in] Comp. class/struct.
                              Datatype&   arr[],   ///<[in,out] Array to search.
                              INDEX       iEnd,    ///<[in] Ending index.
                              INDEX       iBeg=0)  /**<[in] Starting index. */{
   // This is more complicated than a standard insertion sort because array
   // access in MT4 is 10x slower than variable access.
   Datatype placing;
   Datatype previous = arr[iBeg];
   for(INDEX iPlace = iBeg + 1; iPlace < iEnd; ++iPlace){
      placing  = arr[iPlace];                   // Where does this go?
      if(comp.is_before(previous, placing) ){   // Already in correct position.
         previous = placing;                    // Prime for next iteration.
      } else {
         INDEX    iEmpty = iPlace;              // Value in placing.
         do{
            arr[iEmpty] = previous;             // Move up.
            if(--iEmpty == iBeg) break;         // No below.
            previous = arr[iEmpty - 1];         // Next one.
         } while(comp.is_before(placing, previous) );
         arr[iEmpty] = placing;                 // Insert.
         previous = arr[iPlace];                // Prime for next iteration.
}  }  }
Compiled but not tested.
/
To add comments, please log in or register