Quicksort on a container

Source: Internet
Author: User

 

Quicksort
On a containergood evening

Today, I wanted to fix the sorting in the dynamics
Ax 4.0x Active Directory user import wizard. I wanted the ad users sorted
Their windows alias. There are mutiple tools in Dax to sort records (in
Particle: The recordsortedlist and temporary tables) but I couldn't find
Anything to sort lists, arrays or containers. At first I thought of using
. NET Framework's collections. sortedlist, but the sortedlist. Add () method didn't
Seem to work. So I dug up the quicksort algorithm from a Java website and coded
The following method.

'_ Qsc' is basically a container that contains one
Or comment '[STR]'. It wowould look like this '[[STR], [STR], [STR],...]'.
Reason I didn't implement this with a simpler '[STR,...]' Is that I
Wanted to be able to sort a container by an STR key, but still allowing
Another value or set of values to follow.

You cocould easily adapt
Code to sort by 'str' a container that wocould look like this '[[STR, anytype],
[STR, anytype], [STR, anytype],...] '. That wocould make it a kind
Map (types: String, types: anytype). You can put the following method in your
Global class if you want to call it from anywhere.

Also note that
Containers are passed by value, so this will potentially take a lot of memory
For containers larger than a few hundred elements. Implementing the sorting
Using a class like Dax's array class wocould definitely take less RAM at runtime
Since only one instance of the class wocould be needed throughout (you wowould need
To modify my method though as you wowould need to remove the return type and
Return value since Methods recursive by reference don't need to return
Anything ).

Note: 'gl00mi' from axforum.info rightfully
Suggested I use a map instead of containers since map keys are always sorted (I
Had never noticed), as my following Code creates multiple container duplicates
And junks up memory.
Public static container quicksortstrcontainer (
Container
_ Qsc,
Int _ qsstart = 1,
Int _ qsend = conlen (_ qsc ))
{
Int
Qsi = _ qsstart
Int QSJ = _ qsend;
STR qsx;
STR qskey;

;

[Qsx] = conpeek (_ qsc, (_ qsstart + _ qsend)/2 );

Do

{
[Qskey] = conpeek (_ qsc, qsi );
While (qskey <
Qsx)
{
Qsi ++;
If (qsi <=
Conlen (_ qsc ))
{
[Qskey] = conpeek (_ qsc,
Qsi );
}
Else
{

Break;
}
}

[Qskey] = conpeek (_ qsc,
QSJ );
While (qskey> qsx)
{

QSJ --;
If (qsi> 0)
{

[Qskey] = conpeek (_ qsc, QSJ );
}

Else
{
Break;
}

}

If (qsi <= QSJ)
{
[Qskey] =
Conpeek (_ qsc, qsi );
_ Qsc = conpoke (_ qsc, qsi, conpeek (_ qsc,
QSJ ));
_ Qsc = conpoke (_ qsc, QSJ, [qskey]);

Qsi ++;
QSJ --;
}
}
While (qsi <=
QSJ );

If (_ qsstart <QSJ)
{
_ Qsc =
Quicksortstrcontainer (_ qsc, _ qsstart, QSJ );
}
If (qsi <
_ Qsend)
{
_ Qsc = quicksortstrcontainer (_ qsc, qsi,
_ Qsend );
}

Return _ qsc;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.