NAME

arrcmp - lexicographical comparison of two arrays

SYNOPSIS

arr0.arrcmp(arr1)

DESCRIPTION

The arrcmp method compares two arrays lexicographically (L-compare). It is conceptually similar to C/C++ strcmp(). Two arrays are L-identical if their shapes are identical and every element of both arrays is equivalent.

To define the L-less and L-greater relationships, the elements are considered to have significance. In a vector, elements closer to the beginning have higher significance than elements farther from the beginning. So, the result of the L-comparison depends on the most-significant elemement n of arr0 that is not identical to the corresponding element of arr1. If arr0[n] is less than arr1[n] then arr0 is L-less than arr1. Otherwise it is L-greater.

For multi-dimensional arrays, the concept of significance is applied recursively. For example, with 2-dimensonal arrays, each row is L-compared and the most- significant row which is not equivalent determines whether arr0 is L-less than arr1 or L-greater.

If the shapes of the two arrays are not identical, the smaller dimension in each axis is conceptually extended with appropriate zeros.

Type conversion is applied to numeric types.

RETURN VALUE

Returns 0 if arr0 and arr1 are L-identical
Returns -1 if arr0 is L-less than arr1
Returns 1 if arr0 is L-greater than arr1

ERRORS

TypeCheck if either arr0 or arr1 is not comparable

EXAMPLE

    a = [1,2,3]
    b = [1,2,4]
    a.arrcmp(b)
-1

    b = [1,2,3,4]
    // This is -1 since a is implicitly extended with a zero
    a.arrcmp(b)
-1
    a = [2,3].iterate()
    b = [2,3].iterate()
    b[1,1] = 100
    a
0 1 2
3 4 5

    b
0   1 2
3 100 5

    a.arrcmp(b)
-1

    a[0,2] = 200
    a
0 1 200
3 4   5

    // Since the most significiant differing element
    // is 200, it determines the result of the arrcmp
    a.arrcmp(b)
1

    // Type conversions are performed
    [1,2,3].arrcmp([1.,2.,3])
0

    // Heterogeneous lists and arrays can be compared
    a = {"one","two","three"}
    b = {"one","two hundred", "three"}
    a.arrcmp(b)
-1

SEE ALSO

shape