Array - Commands

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Arrays and Data-Structures > Array -Commands > !FP-Arrays >

Array - Commands

ARR.Sort FP

Previous Top Next


MiniRobotLanguage (MRL)

 

ARR.Sort FP

Sort a Floating-Point Array with Tag-Along Array

 

 

clip1159

Sort a specified array of floating-point numbers in ascending or descending order, with a tag-along array sorted in parallel.

 

Intention

The ARR.Sort FP command sorts a specified array as a floating-point array in ascending (default) or descending order, with a tag-along array sorted in parallel to maintain correspondence between elements. The tag-along array is automatically initialized with string indices ("0", "1", ...) if it is not dimensioned or has a mismatched size; otherwise, its existing content is preserved and sorted. The sorting behavior can be customized using an optional options string specifying direction (A or D). A status code (1 for success, 0 for failure) is returned on the Top of Stack (TOS). All array elements are stored as strings, with floating-point values in MKE$ format for numerical sorting.

 

Can only be used for FP arrays (MKE$ format)! For string arrays, use ARR.Sort. For integer arrays, use ARR.Sort INT.

 

Array Restriction: The sort array and tag-along array must be different (0-32).

Sort Options: Supports ascending (A) or descending (D) order for numerical sorting.

Return Value: A status code (1 for success, 0 for failure) is pushed to the TOS.

Tag-Along Array: The tag-along array is initialized with string indices if not dimensioned or mismatched; otherwise, its content is preserved and sorted in sync with the sort array.

 

Schematic (Array Sorting)

Sort Array: [3.14, 1.23, 2.718] (in MKE$ format)

Tag-Along Array: ["0", "1", "2"] (auto-initialized if not set)

Command: ARR.Sort FP|0|1|A

Result Sort Array: [1.23, 2.718, 3.14] (in MKE$ format)

Result Tag-Along Array: ["1", "2", "0"]

TOS: 1 (success)

 

Syntax

ARR.Sort FP|P1|P2[|P3]

 

Parameter Explanation

 

P1 - Sort Array Number: The array number (0-32) to sort, containing floating-point numbers in MKE$ format.

P2 - Tag-Along Array Number: The array number (0-32) for the tag-along array (must be different from P1).

P3 - Options (Optional): A string containing A (ascending, default) or D (descending). If omitted, defaults to ascending.

 

Examples

'***********************************

' Example 1: Sort floating-point numbers in ascending order

'***********************************

ARR.Clm|0

ARR.Clm|1

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

ARR.Set FP Array|0|2|2.718

ARR.Sort FP|0|1|A

' Array 0 contains: [1.23, 2.718, 3.14] (in MKE$ format)

' Array 1 contains: ["1", "2", "0"]

ARR.Get FP Array|0|0|$$RET

MBX.$$RET

ENR.

 

'***********************************

' Example 2: Sort floating-point numbers in descending order with tag-along

'***********************************

ARR.Clm|0

ARR.Clm|1

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

ARR.Set FP Array|0|2|2.718

ARR.Set|1|0|a

ARR.Set|1|1|b

ARR.Set|1|2|c

ARR.Sort FP|0|1|D

' Array 0 contains: [3.14, 2.718, 1.23] (in MKE$ format)

' Array 1 contains: ["0", "2", "1"]

ARR.Get|1|0|$$RET

MBX.$$RET

ENR.

 

'***********************************

' Example 3: Sort floating-point numbers with tag-along array initialization

'***********************************

ARR.Clm|0

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

ARR.Set FP Array|0|2|2.718

ARR.Sort FP|0|1

' Array 0 contains: [1.23, 2.718, 3.14] (in MKE$ format)

' Array 1 contains: ["1", "2", "0"]

ARR.Get|1|0|$$RET

MBX.$$RET

ENR.

 

 

'============================================================

' TEST SCRIPT for ARR.Sort FP

' Purpose: Verify ARR.Sort FP functionality with JIV for automated checks.

' Tests ascending sort, descending sort, tag-along preservation, tag-along

' initialization, different array numbers, and edge cases for MKE$ formatted arrays.

'============================================================

' Initialize counters

$$PAS=0

$$FAI=0

STS.CLEAR

PRT. ===================================================

PRT. Test 1.1: Sort floating-point in ascending order (Array 0, Tag Array 1)

STS.CLEAR

ARR.Clm|0

ARR.Clm|1

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

ARR.Set FP Array|0|2|2.718

PRT.----------------

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Sort FP|0|1|A

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Get FP Array|0|0|$$RET

$$EXP=1.23

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error1

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next1

:Lab_Error1

GSB.Test

'-----------------------------------------------------------

:Lab_Next1

PRT.########################################################

PRT.########################################################

PRT. Test 1.2: Sort floating-point in descending order with tag-along preservation

STS.CLEAR

ARR.Clm|0

ARR.Clm|1

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

ARR.Set FP Array|0|2|2.718

ARR.Set|1|0|a

ARR.Set|1|1|b

ARR.Set|1|2|c

PRT.----------------

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Sort FP|0|1|D

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Get|1|0|$$RET

$$EXP=a

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error2

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next2

:Lab_Error2

GSB.Test

'-----------------------------------------------------------

:Lab_Next2

PRT.########################################################

PRT.########################################################

PRT. Test 1.3: Sort floating-point with tag-along initialization

STS.CLEAR

ARR.Clm|0

ARR.Clm|1

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

ARR.Set FP Array|0|2|2.718

PRT.----------------

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Sort FP|0|1

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Get|1|0|$$RET

$$EXP=1

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error3

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next3

:Lab_Error3

GSB.Test

'-----------------------------------------------------------

:Lab_Next3

PRT.########################################################

PRT.########################################################

PRT. Test 1.4: Sort using different array numbers (Array 10, Tag Array 20)

STS.CLEAR

ARR.Clm|10

ARR.Clm|20

ARR.Set FP Array|10|0|5.67

ARR.Set FP Array|10|1|2.34

ARR.Set FP Array|10|2|4.56

ARR.Set|20|0|x

ARR.Set|20|1|y

ARR.Set|20|2|z

PRT.----------------

ARR.ShowFP|10

ARR.ShowFP|20

PRT.----------------

ARR.Sort FP|10|20|A

ARR.ShowFP|10

ARR.ShowFP|20

PRT.----------------

ARR.Get FP Array|10|0|$$RET

$$EXP=2.34

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error4

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next4

:Lab_Error4

GSB.Test

'-----------------------------------------------------------

:Lab_Next4

PRT.########################################################

PRT.########################################################

PRT. Test 1.5: Empty array handling

STS.CLEAR

ARR.Clm|0

ARR.Clm|1

PRT.----------------

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Sort FP|0|1|A

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Get|0|0|$$RET

$$EXP=

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error5

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next5

:Lab_Error5

GSB.Test

'-----------------------------------------------------------

:Lab_Next5

PRT.########################################################

PRT.########################################################

PRT. Test 1.6: Invalid array number (sortArrayNum > 32)

STS.CLEAR

ARR.Clm|0

ARR.Clm|1

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

PRT.----------------

ARR.ShowFP|0

PRT.----------------

ARR.Sort FP|33|1|A

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Get FP Array|0|0|$$RET

$$EXP=3.14

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error6

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next6

:Lab_Error6

GSB.Test

'-----------------------------------------------------------

:Lab_Next6

PRT.########################################################

PRT.########################################################

PRT. Test 1.7: Same array number for sort and tag-along

STS.CLEAR

ARR.Clm|0

ARR.Set FP Array|0|0|3.14

ARR.Set FP Array|0|1|1.23

PRT.----------------

ARR.ShowFP|0

PRT.----------------

ARR.Sort FP|0|0|A

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Get FP Array|0|0|$$RET

$$EXP=3.14

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error7

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next7

:Lab_Error7

GSB.Test

'-----------------------------------------------------------

:Lab_Next7

PRT.########################################################

PRT.########################################################

PRT. Test 1.8: Invalid MKE$ format in sort array

STS.CLEAR

ARR.Clm|0

ARR.Clm|1

ARR.Set|0|0|invalid ' Not MKE$ format

ARR.Set FP Array|0|1|1.23

ARR.Set|1|0|a

ARR.Set|1|1|b

PRT.----------------

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Sort FP|0|1|A

ARR.ShowFP|0

ARR.ShowFP|1

PRT.----------------

ARR.Get|1|0|$$RET

$$EXP=a

VBT.$$RET

VBT.$$EXP

JIV.$$RET!$$EXP|Lab_Error8

PRT. -> PASS

VIC.$$PAS

JMP.Lab_Next8

:Lab_Error8

GSB.Test

'-----------------------------------------------------------

:Lab_Next8

PRT. ===================================================

PRT. TEST SUMMARY

PRT. ===================================================

CAL.$$TOT=$$PAS+$$FAI

$$MSG=Total Tests: $$TOT

PRT.$$MSG

$$MSG=Passed: $$PAS

PRT.$$MSG

$$MSG=Failed: $$FAI

PRT.$$MSG

JIV.$$FAI=0|Lab_Success

$$MSG=FAILURE: $$FAI of $$TOT tests failed.

MBX.$$MSG|Test Result|16

JMP.Lab_End

:Lab_Success

MBX.SUCCESS: All tests passed!|Test Result|64

:Lab_End

ENR.

:Test

$$MSG= -> FAIL - Result: $$RET (exp: $$EXP)

PRT.$$MSG

VIC.$$FAI

RET.

 

 

 

Remarks

- The command sorts a specified array of floating-point numbers in MKE$ format, with a tag-along array sorted in parallel. The tag-along array is initialized with string indices if not dimensioned or mismatched; otherwise, its content is preserved.

- Use ARR.Swap to move another array to a desired position for sorting.

- Options are case-insensitive (e.g., "D" or "d").

- The TOS returns 1 for success, 0 for failure (e.g., invalid array number, invalid MKE$ format).

- Empty arrays return success (TOS = 1) with no changes.

- Invalid MKE$ strings in the sort array are treated as 0 during sorting.

 

Limitations

- Array numbers are limited to 0–32, and the sort and tag-along arrays must be different.

- Requires 2 or 3 parameters; fewer or more result in no operation.

- No support for multidimensional arrays.

 

See also:

ARR.Set

ARR.Get

ARR.Clr

ARR.Swap

ARR.Dim

ARR.Sort

ARR.Sort INT