Wednesday 25 February 2015

Sort container and Array for Dynamics AX

Just a quick snippet for sorting container and Array object.

//Method to sort container
static container sortContainer(container _con)
{
    anytype     temp1;
    anytype     temp2;
    int         i;
    int         j;
    container   sortedCon;
    ;

    sortedCon = _con;

    // Sort the container
    for (i = 1; i <= conlen(sortedCon); i++)
    {
        for (j = i + 1; j <= conlen(sortedCon); j++)
        {
            temp1 = conpeek(sortedCon, j);
            temp2 = conpeek(sortedCon, i);

            if (temp1 < temp2)
            {
                sortedCon = condel(sortedCon, j, 1);
                sortedCon = conins(sortedCon, j, temp2);
                sortedCon = condel(sortedCon, i, 1);
                sortedCon = conins(sortedCon, i, temp1);
            }
        }
    }

    return sortedCon;
}

//Method to sort Array
static Array sortArray(Array _array)
{
    int         i;
    int         arrayCount = _array.lastIndex();
    container   con, sortedCon;

    //Switch from Array to container
    for(i = 1; i <= arrayCount; i++)
    {
        con = conIns(con, i, _array.value(i));
    }
    
    //Sort the container
    sortedCon = sortContainer(con);
    
    //Switch from container to Array
    for(i = 1; i <= conLen(sortedCon); i++)
    {
        _array.value(i, conPeek(sortedCon, i));
    }
    
    return _array;
}

2 comments:

  1. found quick sort here: https://gatesasbait.wordpress.com/2007/12/15/quicksort-on-a-container/
    and modify to soft two-values-container (like [["A",5],["B", 8],["C", 1],["D",6]] ), by second value:
    public static container quickSort2Val(
    container _qsc,
    int _qsstart = 1,
    int _qsend = conlen(_qsc))
    {
    int qsi = _qsstart;
    int qsj = _qsend;
    int qsx;str qsV;
    int qsKey;
    str qsVal;
    ;

    [qsV,qsx] = conpeek(_qsc, (_qsstart +_qsend)/2);

    do
    {
    [qsVal,qsKey] = conpeek(_qsc, qsi);
    while (qsKey < qsx)
    {
    qsi++;
    if (qsi <= conlen(_qsc))
    {
    [qsVal,qsKey] = conpeek(_qsc, qsi);
    }
    else
    {
    break;
    }
    }

    [qsVal, qsKey] = conpeek(_qsc, qsj);
    while (qsKey > qsx)
    {
    qsj--;
    if (qsi > 0)
    {
    [qsVal, qsKey] = conpeek(_qsc, qsj);
    }
    else
    {
    break;
    }
    }

    if (qsi <= qsj)
    {
    [qsVal, qsKey] = conpeek(_qsc, qsi);
    _qsc = conpoke(_qsc, qsi, conpeek(_qsc, qsj));
    _qsc = conpoke(_qsc, qsj, [qsVal, qsKey]);
    qsi++;
    qsj--;
    }
    }
    while (qsi <= qsj);

    if (_qsstart < qsj)
    {
    _qsc = quickSort2Val(_qsc, _qsstart, qsj);
    }
    if (qsi < _qsend)
    {
    _qsc = quickSort2Val(_qsc, qsi, _qsend);
    }

    return _qsc;
    }

    ReplyDelete
  2. Thanks for sharing such a good post.
    Whenever you have issues with your dynamics software, you can always give anegis consulting a call, or find any other dynamics partner. I'm sure they will sort out all your problems.

    ReplyDelete