# EssOtlSortChildren

Description

EssOtlSortChildren() sorts the children of an outline member.

Syntax

    ESS_FUNC_M EssOtlSortChildren (hOutline, hParent, usType, fpCompare, pUserData);

ESS_HOUTLINE_T | hOutline
ESS_HMEMBER_T | hParent
ESS_USHORT_T | usType
ESS_POTLSORTFUNC_T | fpCompare
ESS_PVOID_T | pUserData

Parameters

hOutline | Outline context handle.
hParent | Handle of parent of the children to sort. If this is ESS_NULL, the dimensions are sorted.
usType | Sort type. This can be one of the following: ESS_SORT_ASCENDING ESS_SORT_DESCENDING ESS_SORT_USERDEFINED
fpCompare | Pointer to user-defined comparison function. This is only used if the usType parameter is ESS_SORT_USERDEFINED. It points to a function defined as follows: ESS_INTFUNC_M Compare( ESS_HMEMBER_T mbr1, ESS_HMEMBER_T mbr2, ESS_PVOID_T pUserData); The function accepts handles for two members and should return the following: < 0 if mbr1 goes before mbr2. = 0 if mbr1 is equivalent to mbr2. > 0 if mbr1 goes after mbr2.
pUserData | Pointer to any user-specified data. This is only used if the usType parameter is ESS_SORT_USERDEFINED. Each time the comparison function is called, the value of this parameter is passed into the comparison function.

Return Value

Returns 0 if successful; otherwise one of the following:

OTLAPI_BAD_SORTTYPE
OTLAPI_BAD_SORTCOMPAREFUNC
OTLAPI_SORT_TOOMANY

Notes

- In Windows, you should call MakeProcInstance() to get a pointer to the compare function.
- During the callback function, you should not call any outline functions that might change the outline. Only EssOtlGetMemberInfo(), EssOtlGetMemberFormula(), and EssOtlGetMemberAlias() can be called.

Example

    #include <essapi.h>
    #include <essotl.h>

    ESS_STS_T          sts = 0;
    ESS_HOUTLINE_T     hOutline;
    ESS_HMEMBER_T      hMeasures;
    FARPROC            pfnSort;
    ESS_OBJDEF_T       Object;
    ESS_APPNAME_T      szAppName;
    ESS_DBNAME_T       szDbName;
    ESS_OBJNAME_T      szFileName;

    memset(&Object, '\0', sizeof(Object));
    Object.hCtx = hCtx;
    Object.ObjType = ESS_OBJTYPE_OUTLINE;
    strcpy(szAppName, "Sample");
    strcpy(szDbName, "Basic");
    strcpy(szFileName, "Basic");
    Object.AppName = szAppName;
    Object.DbName = szDbName;
    Object.FileName = szFileName;

    sts = EssOtlOpenOutline(hCtx, &Object, ESS_TRUE,
         ESS_TRUE, &hOutline);

    if (!sts)
    {
       sts = EssOtlFindMember(hOutline, "Measures",
             &hMeasures);
    }

    /* User defined sort */
    pfnSort = MakeProcInstance((FARPROC)SortCompare, hInst);

    if (!sts)
    {
       sts = EssOtlSortChildren(hOutline, hMeasures,
             ESS_SORT_USERDEFINED,
             (ESS_POTLSORTFUNC_T)pfnSort,
             (ESS_PVOID_T)hOutline);
    }

    FreeProcInstance(pfnSort);

    /***********************************************/
    int  ESS_INTFUNCT_M SortCompare (
         ESS_HMEMBER_T hMember1,
         ESS_HMEMBER_T hMember2,
         ESS_PVOID_T pData)
    {
         int nRet = 0;
         int nLen1;
         int nLen2;
         ESS_STS_T sts = 0;
         ESS_PMBRINFO_T pMbrInfo1 = ESS_NULL;
         ESS_PMBRINFO_T pMbrInfo2 = ESS_NULL;
         ESS_HOUTLINE_T hOutline =
    (ESS_HOUTLINE_T)pData;

       sts = EssOtlGetMemberInfo(hOutline, hMember1,
             &pMbrInfo1);

       if (!sts && pMbrInfo1)
       sts = EssOtlGetMemberInfo(hOutline,
             hMember2, &pMbrInfo2);

       if (!sts && pMbrInfo2)
       {
         nLen1 = strlen(pMbrInfo1->szMember);
         nLen2 = strlen(pMbrInfo2->szMember);
         if (nLen1 < nLen2)
             nRet = -1;
         else if (nLen1 > nLen2)
             nRet = 1;
       }

       if (pMbrInfo1)
       {
       EssFree(hInst, pMbrInfo1);
       }

       if (pMbrInfo2)
       {
       EssFree(hInst, pMbrInfo2);
       }
       return (nRet);
    }

See Also

EssOtlFindMember()