Money is important, Love is essential.
Can MT4 sort a data array from largest to smallest? 3 replies
How to sort an array? 35 replies
Two Dimensional Array vs One Dimensional Array 13 replies
Calculate Stochastic Formula on a array (non indicator buffer array) 2 replies
How to define the length of an array with an extern variable? 5 replies
QuoteDisliked//+------------------------------------------------------------------+
//| Function..: StringArraySort |
//| Based on..: BenchStringArraySort.mq4 by http://www.MetaQuotes.net|
//| and modified to handle two dimensional arrays and |
//| descend mode. |
//| Parameters: A - Name of the string array. |
//| iStart - Start row, the default value is row zero if |
//| the parameter is omitted. |
//| iCount - Count of rows to be sorted, defaults to the |
//| whole array if the parameter is not passed |
//| or passed as 0 (zero). |
//| iDir - Array sorting direction. It can be either |
//| of the following values: |
//| MODE_ASCEND (1) - sort ascending A to Z, |
//| MODE_DESCEND (2) - sort descending Z to A. |
//| Purpose...: Sort the two dimensional array by the first column. |
//| Returns...: The sorted string array passed by reference. |
//| Sample....: string A[2][2]={"GBPUSD","500","EURUSD","600"}; |
//| StringArraySort(A); |
//| StringArrayWrite(A); |
//+------------------------------------------------------------------+
void StringArraySort(string& A[][], int iStart = 0, int iCount = WHOLE_ARRAY, int iDir = MODE_ASCEND) { int iRows = ArrayRange(A, 0); //---- if(iCount == WHOLE_ARRAY) iCount = iRows; else iCount = (iStart + iCount) - 1; //---- if(iRows == 0 || iCount-iStart <= 0) return (0); int iCols = ArraySize(A) / iRows; string pivot[]; //---- if(ArrayResize(pivot, iCols) == 0) return (ERROR); //---- if(iDir == MODE_ASCEND) StringArraySortAZ(A, iStart, iCount, iCols, pivot); else StringArraySortZA(A, iStart, iCount, iCols, pivot); }
void SortStringArrByLength(string _orig[], string &_sorted[]) { int sz = ArraySize(_orig); ArrayResize(_sorted, sz); int x, i; int tmp[][2]; ArrayResize(tmp, sz); for (i = 0; i < sz; i++) { tmp[i][0] = StringLen(_orig[i]); tmp[i][1] = i; } ArraySort(tmp); for (i = 0; i < sz; i++) { x = tmp[i][1]; _sorted[i] = _orig[x]; } return; } //+------------------------------------------------------------------+