× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Sorta is not a "Stable" sort.
Qsort is also not a "Stable" sort.

Which basically means if you sort the data once by one column, then again by different column, the end result will only be guaranteed to be sorted by the column chosen in the last sort operation.

With sorta you can only sort by one field. You can get around this limitation by combining multiple fields into one new field and then using Sorta on that field.

D UDFNoListN DS Qualified
D UDFNoEntry 100 DIM(100)
D UDFNoListS 44 Overlay(UDFNoEntry:*Next)
D UDFNoAppSeq 6S 0 INZ(*hival)Overlay(UDFNoEntry:*Next)
D UDFSORTFIELD 50 Overlay(UDFNoEntry:*Next)

FOR IDX = 1 TO 100;
// you must add your fields in the order you want them sorted.
UDFSORTFIELD(IDX) = %EDITC(UDFNoAppSeq:'X') + UDFNoListS;
ENDFOR;

SORTA UDFSORTFIELD;


Qsort is nice because you can describe your own Compare function, thus allowing you to sort over any column you want without needing to adjust the data.

D STDINT S 10I 0

D CompareAddr C %PADDR(CompareFields)
D wkDataDS DS QUALIFIED DIM(100)
D FIELD1 44A
D FIELD2 6S 0

// Run Sort
API_QSort(%ADDR(wkDataDS) :%Elem(wkDataDS)
:%SIZE(wkDataDS) :CompareAddr);

P CompareFields B
D CompareFields PI LIKE(STDINT)
D Record1 LIKEDS(wkDataDS) CONST
D Record2 LIKEDS(wkDataDS) CONST
D/FREE
IF Record1.Field2 > Record2.Field2;
Return Greater;
ELSEIF Record1.Field2 < Record2.Field2;
Return Less;
Endif;

IF Record1.Field1 > Record2.Field1;
Return Greater;
ELSEIF Record1.Field1 < Record2.Field1;
Return Less;
Endif;

Return Equal;

/END-FREE
P CompareFields E


Since you only have 100 entries you could also do your own sort. Here's an example of a Bubble sort.

D wkDataDS DS QUALIFIED DIM(100)
D FIELD1 44A
D FIELD2 6S 0

D currdatads DS likeds(Wkdatads)
D priordatads DS likeds(Wkdatads)

D IDX S 10I 0
D LastSwapped S 10I 0
D LeftToSwap S 10I 0


Lefttoswap = %Elem(Wkdatads);
DOW LeftToSwap > 1;
LastSwapped = 0;

// Get First indexed value;
priordatads = wkDataDS(1);

FOR IDX = 2 TO LeftToSwap;
currdatads = wkDataDS(idx);

IF priordatads.Field2 > currdatads.Field2
Or priordatads.Field2 = currdatads.Field2
And priordatads.Field1 > currdatads.Field1;

wkDataDS(idx) = priordatads;
wkDataDS(idx-1) = currdatads;
// Save Swapped index so we know when we can stop sooner.
Lastswapped = idx;
LastSwapped = IDX;
// priordatads is left unchanged because it now contains element IDX

ELSE;
// No swap. Save the Current Record as Prior Record
priordatads = currdatads;
ENDIF;
ENDFOR;
// We no longer need to check the items between Lefttoswap and Total Elements.
LeftToSwap = LastSwapped;
ENDDO;



Chris Hiebert
Senior Programmer/Analyst
Disclaimer: Any views or opinions presented are solely those of the author and do not necessarily represent those of the company.

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.