MinArray
See Also: Array Functions, Array Variable Assignments, Working with Arrays
Purpose
Returns the index of the array element with the lowest (minimum) value.
Return Type
Syntax
MinArray({ArrayId})
MinArray({ArrayId} [, {ObjectId}, {MessageId}])
Where:
-
{ArrayId} is the id of the array being checked. The array must be non-jagged and one-dimensional (it can be a single dimension of a multi-dimensional array).
-
{ObjectId} is the id of the object that implements the comparison function.
-
{MessageId} is the id of the function that will be used to compare two array elements.
What it Does
MinArray returns the index of the array element with the lowest value, which is determined using either an internal sorting algorithm or one provided by the developer.
DataFlex implements different sorting algorithms for different data types in the runtime to provide developers with the most efficient sorting method based on the data type. Algorithms provided in the runtime will also run faster. To use the internal sorting algorithms, simply do not pass the {ObjectId} and {MessageId} parameters. We recommend the use of these internal algorithms whenever there is no strong reason for a developer to use a special comparison.
Custom Comparison Function
The developer has the option of providing a custom comparison function for the sorting process if a custom function is desired or required (for arrays of data types that cannot be sorted using the internal algorithms: struct, RowId, variant).
The function provided in {MessageId} must follow the following syntax:
Function MyCompareFunc {array-element-type} arg1 {array-element-type} arg2 Returns Integer
This function must return one of these values:
-
(GT) - if the first parameter is greater than the second parameter.
-
(EQ) - if the parameters are considered equal.
-
(LT) - if the first parameter is less than the second parameter.
You can use a single custom comparison function for a specific data type for all array functions that require one: BinarySearchArray, BinarySearchInsertPos, CountArray, MinArray, MaxArray, SearchArray, SortArray.
Example
Retrieving the Lowest Value from an Array of Strings
This sample returns the index of the array element with the lowest string value in a dynamic array of strings (sCustomers), then displays the index and the value of the associated array element to the screen.
// fires when the button is clicked
Procedure OnClick
String[] sCustomers
Integer iMaxCustomer
// populate sCustomers with customer data
Move "Smith, Janet" to sCustomers[0]
Move "Rodriguez, Pedro" to sCustomers[1]
Move "Smith, Judy" to sCustomers[2]
Move "Jones, Fred" to sCustomers[3]
Move "Anderson, Martin" to sCustomers[4]
Move "Schmidt, Michael" to sCustomers[5]
Move "Verne, Jacques" to sCustomers[6]
Move "Ricci, Enrico" to sCustomers[7]
Move "Sorensen, Karl" to sCustomers[8]
Move "Garcia, Juan" to sCustomers[9]
// get min array index
Move (MinArray(sCustomers)) to iMaxCustomer
// display the min array index and that element's value
Showln "Lowest value: " (String(iMaxCustomer)) ": " (sCustomers[iMaxCustomer])
End_Procedure
Retrieving the Lowest Value from an Array of Structs
This sample declares a structured data type (tUSAddress), then declares an array of type tUSAddress of 5 elements. The first array (myAddresses) is populated with zip codes (since we are only sorting using zip codes, we are not filling the other struct members for this sample).
A custom comparison function is used to determine how array elements are sorted. Last, the index of the array element with the lowest zip code value is displayed to the screen, along with the value of the element.
Struct tUSAddress
String Street
Integer ZipCode
End_Struct
// Custom comparison function:
// Returns (LT) if struct value in first parameter < struct value in second parameter.
// Returns (GT) if struct value in first parameter > struct value in second parameter.
// Otherwise returns (EQ).
Function CompareAddresses tUSAddress address1 tUSAddress address2 Returns Integer
Integer iRetVal
If (address1.ZipCode > address2.ZipCode) ;
Function_Return (GT)
If (address1.ZipCode < address2.ZipCode) ;
Function_Return (LT)
Function_Return (EQ)
End_Function
// fires when the button is clicked
Procedure OnClick
tUSAddress[5] myAddresses
Integer iMaxIndex
// initialize address array
Move 33186 to myAddresses[0].ZipCode
Move 33177 to myAddresses[1].ZipCode
Move 90210 to myAddresses[2].ZipCode
Move 10245 to myAddresses[3].ZipCode
Move 78610 to myAddresses[4].ZipCode
// get the min element's index
Move (MinArray(myAddresses, Self, (RefFunc(CompareAddresses)))) to iMaxIndex
// display lowest element index and value
Showln "The array element with the lowest zip code is element " ;
iMaxIndex " with zip code " myAddresses[iMaxIndex].ZipCode
End_Procedure
Retrieving the Lowest Value from a Single Dimension of a Multi-Dimensional Array
This sample declares a 2-dimensional string array and fills it with 8 (2x4) elements of unsorted string values. This creates an array as such:
Smith Rodriguez Scott Jones
Anderson Smith Verne Ricci
The minimum value in the first "row" (row 0) of the array is determined and displayed, then the minimum value in the second "row" (row 1).
Procedure OnClick
String[][] sCustomers
String sSearchTerm
Integer iCount0 iCount1
Move "Smith" to sCustomers[0][0]
Move "Rodriguez" to sCustomers[0][1]
Move "Scott" to sCustomers[0][2]
Move "Jones" to sCustomers[0][3]
Move "Anderson" to sCustomers[1][0]
Move "Schmidt" to sCustomers[1][1]
Move "Verne" to sCustomers[1][2]
Move "Ricci" to sCustomers[1][3]
Move (MinArray(sCustomers[0])) to iCount0
Showln "Min element value in first row of array: " ;
(String(iCount0)) " (" sCustomers[0][iCount0] ")"
Move (MinArray(sCustomers[1])) to iCount1
Showln "Min element value in second row of array: " ;
(String(iCount1)) " (" sCustomers[1][iCount1] ")"
End_Procedure