# sorting 2 dimensional array by second dimension - page 3

803

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.

476

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.

287

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]);
}
}```
287

```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....
16764

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; kk<COGLINES; kk++) { // Cog.EMA, Cog.EMA.prev and COG.ticket
for (int ii=kk; ii>0 && 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.

int Buy.Ticket[] and double Buy.Price[] is easier understood and contains the correct datatype.
1304

LEHayes:

I am really tired and worn out from trying to push the release of about 5 strategies out the door, so this became a little of a nice-ity instead of a neccessity.

I am willing to make a deal with anyone who can write this function for me. I will give you your choice of products with a years license, for free for helping me out on this and too boot, I will give you a personal copy of this hedging strategy for free unlimited lifetime use. I am just too swamped to do it all and my dev team is heavily burdened with releasing what is already on our plates. I think the average sleep value across the team is about 4 to 6 hours a day.

Obviously I'm not the one that can readily do this for you, though I suppose as I'm climbing the hill on Arrays; but I'm still tripping over potholes and rocks in the parking lot.... . . .

From what u have & other knowledgeable programmers here have hashed out you have a solution, albeit a kluge: just need someone to take the time and do it.

Additionally: there are numerous mql 4 <=> Excel interfaces around. Though Excel v2007 has a bug in it that won't allow the sheet to be enlarged after it is made: just a LITTLE inconvenience: NOT! Very lame to not have fixed it as of yet it, but in true MS fashion, probably going to leave it in and then use it as added inducement to buy the latest v201x when it comes out.

U have that many products siting on the shelf ready to go, maybe it's time to switch hats and get out of the coding cage for a while and focus on some marketing and sales?

476

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

145

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