Define the Collection and Adding Items to It

Visual LANSA

Define the Collection and Adding Items to It

A sorted array collection is defined In the reusable part used to sort the employees. Employee details are retrieved from file PSLMST,  #SortedEmployee objects are created based on the #Employee object and assigned the retrieved employee details. The collection is then filled with the #SortedEmployee objects:

Set_Ref Com(#SortedEmployeeList) To(*Create_as #Prim_SACO<#Employee>)
 
Select Fields(#EMPNO #GIVENAME #SURNAME #DEPTMENT #SALARY #POSTCODE) From_File(PSLMST)
Set_Ref Com(#SortedEmployee) To(*Create_as #Employee)
Set Com(#SortedEmployee) Pempno(#Empno) Pgivename(#GiveName) Psurname(#Surname) Psalary(#Salary) Pdepartment(#Deptment) Ppostcode(#PostCode)
Invoke Method(#SortedEmployeeList.Insert) Item(#SortedEmployee)
Endselect

 

As every item is added to the collection, the Compare event is triggered. This event is used to provide the logic for sorting the collection. #ObjectEmployee and #SubjectEmployee objects are created to be used as the Object and #Subject parameters to compare the employees:

Evtroutine Handling(#SortedEmployeeList.Compare) Object(#Object) Subject(#Subject) Result(#Result)
Define_Com Class(#Employee) Name(#ObjectEmployee) Reference(*Dynamic)
Define_Com Class(#Employee) Name(#SubjectEmployee) Reference(*Dynamic)
 
* Cast the object and subject objects to the Employee object
 
Set_Ref Com(#ObjectEmployee) To(*Dynamic #Object)
Set_Ref Com(#SubjectEmployee) To(*Dynamic #Subject)
 
* Now compare the objects based on the current #R_OrderBy value
 
Case Of_Field(#R_ORDERBY)
 
When Value_Is('= GiveName')
Invoke Method(#Com_Owner.CompareAlpha) Object(#ObjectEmployee.pGiveName) Subject(#SubjectEmployee.pGiveName) Result(#Result)
 
When Value_Is('= Surname')
Invoke Method(#Com_Owner.CompareAlpha) Object(#ObjectEmployee.pSurName) Subject(#SubjectEmployee.pSurName) Result(#Result)
 
When Value_Is('= Deptment')
Invoke Method(#Com_Owner.CompareAlpha) Object(#ObjectEmployee.pDepartment) Subject(#SubjectEmployee.pDepartment) Result(#Result)
 
When Value_Is('= Salary')
Invoke Method(#Com_Owner.CompareNumeric) Object(#ObjectEmployee.pSalary) Subject(#SubjectEmployee.pSalary) Result(#Result)
 
When Value_Is('= PostCode')
Invoke Method(#Com_Owner.CompareNumeric) Object(#ObjectEmployee.pPostCode) Subject(#SubjectEmployee.pPostCode) Result(#Result)
 
Otherwise
Invoke Method(#Com_Owner.CompareAlpha) Object(#ObjectEmployee.pEmpNo) Subject(#SubjectEmployee.pEmpNo) Result(#Result)
 
Endcase
 
Endroutine

 

Ý Sorted Array Collection Example