# 功能 - 对一个结构数组进行排序的方法。奖金10元

```template <typename T>
void ArrayReindex( T &Array[], const double &TmpSort[][2] )
{
T TmpArray[];

for (int x = ::ArrayResize(TmpArray, ::ArrayRange(TmpSort, 0)) - 1; x >= 0; x--)
TmpArray[x] = Array[(int)(TmpSort[x][1] + 0.1)];

::ArraySwap(Array, TmpArray);

return;
}

// Сортировка массива структур и указателей на объекты по (под-) полю/методу.
#define  ArraySortStruct(ARRAY, FIELD)                                      \
{                                                                          \
double TmpSort[][2];                                                     \
\
for (int x =::ArrayResize(TmpSort, ::ArraySize(ARRAY)) - 1; x >= 0; x--) \
{                                                                        \
TmpSort[x][0] = (double)ARRAY[x].FIELD;                                \
TmpSort[x][1] = x;                                                     \
}                                                                        \
\
::ArraySort(TmpSort);                                                    \
::ArrayReindex(ARRAY, TmpSort);                                          \
}                                  ```

Документация по MQL5: Операции с массивами / ArraySort
• www.mql5.com
//| Получение значений границ для тиковых объемов                    | //| Custom indicator initialization function                         | //| Custom indicator iteration function                              | //

Dmitry Fedoseev:

CArrayObj 是一个简单的数组吗？

Dmitry Fedoseev:

CArrayObj是一个简单的数组吗？

@fxsaber的 变体将是最快的，因为编译器将把宏部署到完整的代码中。

Igor Makanu:

@fxsaber的 变体将是最快的，因为编译器将把宏部署到完整的代码中。

```#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property version   "1.00"
#property strict

struct SMy{
int x1;
int x2;
};

SMy s[3]={{4,44},{2,22},{3,33}};

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){

SortHoareUp(s);

for(int i=0;i<3;i++){
}

}

void SortHoareUp(SMy  &aAr[])
{
HoareUp(aAr,0,ArraySize(aAr)-1);
}

void HoareUp(SMy  &aAr[],int aLeft,int aRight)
{
SMy tmp;
int i=aLeft;
int j=aRight;

int xx=aAr[(aLeft+aRight)/2].x1; // int заменить на тип поля по которому выполняется сравнение

do
{
while(i<aRight && aAr[i].x1<xx)i++;
while(j>aLeft && xx<aAr[j].x1)j--;
if(i<=j)
{
tmp=aAr[i];
aAr[i]=aAr[j];
aAr[j]=tmp;
i++;
j--;
}
}
while(i<=j);
if(aLeft<j)HoareUp(aAr,aLeft,j);
if(i<aRight)HoareUp(aAr,i,aRight);
}```

```#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property version   "1.00"
#property strict

#include <Arrays/ArrayObj.mqh>

class CMyClass:public CObject{
public:
int x1;
int x2;
int x3;
CMyClass(int ax1,int ax2,int ax3){
x1=ax1;
x2=ax2;
x3=ax3;
}
int Compare(const CObject *node,const int mode=0)const{
const CMyClass * t=node;
if(mode==0 && t.x1>this.x1){
return(1);
}
if(mode==1 && t.x1<this.x1){
return(1);
}
return(-1);
}
};

CMyClass * tmp;
CArrayObj a;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){

Al(1);
a.Sort(0);
Al(2);
a.Sort(1);
Al(3);

}
//+------------------------------------------------------------------+

void Al(int n){
for(int i=0;i<a.Total();i++){
tmp=a.At(i);
}
}```

Dmitry Fedoseev:

HH：微软网站上所有基类的C#来源，有多种，它们与SB中的接口类似，但可惜，他们没有最终确定SB--他们说自己做；)

Igor Makanu:

HH：微软网站上的C#与所有基本类的来源，有各种各样的，它们与SB中的接口相似，但可惜的是，他们没有最终确定SB--他们说自己做；)