Sort a container [using X++ , Dynamics AX]

Friends,

Recently I was in need of sorting the elements in the container and use them .

I am not sure how far this code is helpful to you guys. Below code will help to sort the elements in the container. If there is already any function to do this, then kindly ignore.

static void SR_sortContainer(Args _args)
{
container con = [5,1,2,’Sumit Loya’,9, ‘Ashish singh’, NoYes::No];
str temp1;
str temp2;
int i;
int j;
container sorCon;
;

sorCon = con;

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

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

conview(sorCon);
}

Please note: Even though you have different data types in the container, the above code will sort the elements. [Recommended is similar datatypes, next question would be y not use set which result in ascending order but disadvantage is set will not allow duplicates]

Happy Dax’ng.
Good day!!

One Response to “Sort a container [using X++ , Dynamics AX]”

  1. Alex Says:

    This is a basic bubble sort by the way, except you’re dropping off the front end, instead of the back end. Both work exactly the same way.


Leave a comment