Sorting an array by two elements

5 pts.
Tags:
i5
ILE for iSeries
iSeries
RPG
I want to sort and array by data and then by ship via, both elements of an Order Array. Does anyone know a way that I can sort at two levels in the Arraay?
ASKED: November 28, 2007  5:57 PM
UPDATED: March 3, 2010  8:25 AM

Answer Wiki

Thanks. We'll let you know when a new response is added.

The code below is from the ILE RPG programmers guide. I am assuming here you are coding in RPG.

<pre>

ILE RPG Language Reference

<b>Sorting using part of the array as a key</b>

You can use the OVERLAY keyword to overlay one array over another. For example, you can have a base array which contains names and salaries and two overlay arrays (one for the names and one for the salaries). You could then sort the base array by either name or salary by sorting on the appropriate overlay array.

*…1….+….2….+….3….+….4….+….5….+….6….+….7…+….
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++
D DS
D Emp_Info 50 DIM(500) ASCEND
D Emp_Name 45 OVERLAY(Emp_Info:1)
D Emp_Salary 9P 2 OVERLAY(Emp_Info:46)
D
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq….
C
C* The following SORTA sorts Emp_Info by employee name.
C* The sequence of Emp_Name is used to determine the order of the
C* elements of Emp_Info.
C SORTA Emp_Name
C* The following SORTA sorts Emp_Info by employee salary
C* The sequence of Emp_Salary is used to determine the order of the
C* elements of Emp_Info.
C SORTA Emp_Salary
</pre>

I would go to the iSeries Information site for your version and review the ILE RPG Programming Reference guide for any additional details.

The only way you could sort by Emp_Name/Emp_Salary would be if the two fields were next to each other in the array, and you would create a sort field that overlaid them both .

=========================================================

There are at least two ways to sort an array by multiple elements. The more complex way is by use of the <a href=”http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=/apis/QLGSORT.htm”>Sort (QLGSORT) API</a>. Nowadays, the most common method is the <a href=”http://publib.boulder.ibm.com/infocenter/iseries/v5r4/topic/books/sc415607.pdf#QSORT”>Sort Array (qsort()) C API</a>. Here’s one good <a href=”http://www.lisug.org/Techtipqsort.htm”>example of qsort()</a>.

Tom

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following