English

## sorting 2 dimensional array by second dimension

 705 1005phillip 2010.05.03 16:23 #   cameofx wrote >> These are the way I pictured it too. the reference I quoted regarding first-dimension threw me off..(see above post). So we logically cannot resize the ColumnNumber and/or WorksheetNumber and/or BookNumber..only the RowNumber. Which is the left-most pair of bracket in 2D, 3D & 4D arrays. Gordon already confirmed, but since you quoted my example I am compelled to respond as well just to confirm that your interpretation of my post is correct. similar
 416 cameofx 2010.05.04 06:38 #   1005phillip: Gordon already confirmed, but since you quoted my example I am compelled to respond as well just to confirm that your interpretation of my post is correct. That post was intended to reply yours, I forgot to address your name, my mistake :) similar
 414 Russell 2010.06.02 18:39 #   ok old topic, but this code might help someone sorting the second dim. ```int start(){    double ldDest[6][2];    double ldSource[6][2];    ldSource[0][0] = 643;    ldSource[0][1] = 3236;    ldSource[1][0] = 46769;    ldSource[1][1] = 523;    ldSource[2][0] = 234;    ldSource[2][1] = 435;    ldSource[3][0] = 854;    ldSource[3][1] = 3344;    ldSource[4][0] = 8465;    ldSource[4][1] = 2434;    ldSource[5][0] = 1545;    ldSource[5][1] = 341;    _ArraySort2D(ldDest, ldSource, 1);    for (int i = 0; i < ArrayRange(ldDest,0); i++){             Print(i," ",1,"= ",ldDest[i,1]);        } } void _ArraySort2D(double &rdDest[][], double _dSource[][], int _iSortIndex){       int liSize[2];    liSize[0] = ArrayRange(_dSource, 0);    liSize[1] = ArrayRange(_dSource, 1);    int liPosition;       for (int i = 0; i < liSize[0]; i++){       liPosition = 0;       for (int j = 0; j < liSize[0]; j++){          if (_dSource[i,_iSortIndex] > _dSource[j,_iSortIndex]){            liPosition++;          }       }       ArrayCopy(rdDest, _dSource, liPosition*liSize[1], i*liSize[1],  liSize[1]);    } }``` similar
 414 Russell 2010.06.02 18:50 #   ```void _ArraySort2D(double &rdDest[][], double _dSource[][], int _iSortIndex, int _iDirection=MODE_ASCEND){       int liSize[2];    liSize[0] = ArrayRange(_dSource, 0);    liSize[1] = ArrayRange(_dSource, 1);    int liPosition;       for (int i = 0; i < liSize[0]; i++){       switch(_iDirection){          case MODE_ASCEND:             liPosition = 0;             for (int j = 0; j < liSize[0]; j++){                if (_dSource[i,_iSortIndex] > _dSource[j,_iSortIndex]){                  liPosition++;                }             }             ArrayCopy(rdDest, _dSource, liPosition*liSize[1], i*liSize[1],  liSize[1]);             break;          case MODE_DESCEND:             liPosition = liSize[0]-1;             for (j = 0; j < liSize[0]; j++){                if (_dSource[i,_iSortIndex] > _dSource[j,_iSortIndex]){                  liPosition--;                }             }             ArrayCopy(rdDest, _dSource, liPosition*liSize[1], i*liSize[1],  liSize[1]);             break;       }    } }``` and descending....
 13118 WHRoeder 2010.06.02 19:23 #   The alternative to sorting is making an index table.```// Cog[], Cog.EMA[], Cog.EMA.prev[], Cog.ticket[]     int order[COGLINES];                // Keep relative ordering of Cog,     for (int kk=0; kk0 && COG[order[ii-1]] > COG[kk]; ii--) {             order[ii]=order[ii-1];  // kk   cog[kk] order[kk]   cog[order[kk]]         }                           // 0    3.      1           1.         order[ii]=kk;               // 1    1.      2           2.     }                               // 2    2.      0           3. ``` Faster, and allows different data types with understandable variable names in different one dimensional arrays. AryBuy[1][0] contains the ticket, AryBuy[1][1] contains the price. int Buy.Ticket[] and double Buy.Price[] is easier understood and contains the correct datatype. similar
 416 cameofx 2010.06.03 11:13 #   Russel & WHRoeder, Thank you very much. Those would help a lot! I'm a self-taught programmer. You don't know how slow I'm moving if I was to device & do tests on my own (which is how I regularly go to figure out stuff...).  @FourX : Keep pummeling... one array at a time, you'll get there eventually... similar
 48 TT_FX1 2010.11.29 21:20 #   Russell: and descending.... Hi Russell Your suggested algorithm is working pretty well only when there are 2 or more elements are carrying the same value in the arrays, it is unable to do the sorting ..Eg, ldSource[2][1] = 435, ldSource[3][1] = 435. Any suggestion for this kind of case. Thx ``` ldSource[0][0] = 643;    ldSource[0][1] = 3236;    ldSource[1][0] = 46769;    ldSource[1][1] = 523;    ldSource[2][0] = 234;    ldSource[2][1] = 435;    ldSource[3][0] = 854;    ldSource[3][1] = 435; ```