Sort(CalcComparer: ICalcObjectComparer);
CalcComparer. Comparer objects.
The Sort method sorts a list using a comparer.
Executing the example requires that the repository contains a calculation algorithm with the ALGORITHM identifier. The calculation algorithm should contains configured objects.
Add links to the Algo, Collections, Metabase system assemblies. Also, add links to the assemblies required fir working with calculation algorithms.
Sub UserProc;
Var
MB: IMetabase;
MObj: IMetabaseObjectDescriptor;
Algo: ICalcObject;
List: ICalcObjectsList;
CalcAlgo: ICalcAlgorithm;
Obj: ICalcObject;
i, c: Integer;
Begin
MB := MetabaseClass.Active;
// Get calculation algorithm
MObj := MB.ItemById("ALGORITHM");
Algo := CalcObjectFactory.CreateCalcObject(MObj, True);
CalcAlgo := Algo As ICalcAlgorithm;
// Get list of calculation algorithm objects
List := CalcAlgo.Items;
c := List.Count;
Debug.WriteLine("---Before sorting---");
For i := 0 To c - 1 Do
Obj := List.Item(i);
Debug.WriteLine(Obj.Name);
End For;
// Execute sorting using proper sorter
List.Sort(New MyComparer.Create);
Debug.WriteLine("---After sorting---");
For i := 0 To c - 1 Do
Obj := List.Item(i);
Debug.WriteLine(Obj.Name);
End For;
End Sub UserProc;
Class MyComparer: Object, ICalcObjectComparer, IComparer
Public Function Compare(obj1, obj2: Variant): Integer;
Begin
Return CompareCalcObject(obj1, obj2);
End Function Compare;
Public Function CompareCalcObject(objV1, objV2: ICalcObject): Integer;
Var
Comp: IComparer;
Begin
Comp := Comparer.StringComparer;
//Compare names of calculation algorithm objects
Return Comp.Compare(objV1.Name, objV2.Name)
End Function CompareCalcObject;
End Class MyComparer;
After executing the example, the list of calculation algorithm objects is obtained. This list will be sorted by object names using the custom comparer. The list contents before and after sorting will be displayed in the development environment console.
See also: