5 pts.
 Sorting an array by two elements
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?

Software/Hardware used:
ASKED: November 28, 2007  5:57 PM
UPDATED: March 3, 2010  8:25 AM

Answer Wiki:
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
Last Wiki Answer Submitted:  March 3, 2010  8:25 am  by  BigKat   7,175 pts.
All Answer Wiki Contributors:  BigKat   7,175 pts. , WaltZ400   645 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _