cmp()

Read(2227) Label: compare, expression, sequence,

Description:

Compare two expressions or two sequences.

Syntax:

cmp(x, y)

When comparing two expressions x and y, return 0 if they are equal; return 1 if x is greater than y; return -1 if x is less than y

cmp(A{, B})

When comparing two sequences A and B having same number of members, return 0 if all members are equal, otherwise compare members of them in a normal order; for the first members that are not equal, return 1 if the one in A is larger and return -1 if the one in A is smaller. If the number of members in the two sequences are not the same and members in the sequence whose member number is smaller are completely equal to their corresponding ones in the other sequence, then the sequence with less members will be smaller

cmp(A;B)

When the lengths of sequences A and B are equal, compare members of them in normal order; return 1 if a member in A is larger and return -1 if a member in A is smaller and return 0 if all members are equal. If the length of sequence A is less than that of sequence B, return 1 if a member in A is larger and return -1 if a member in A is smaller; return -1 if members of sequence A are equal to their counterparts in sequence B.

When sequences A and B have same number of members, return 0 if all members are equal, otherwise compare members of them in a normal order; for the first members that are not equal, return 1 if the one in A is larger and return -1 if the one in A is smaller

cmp(A,0)

When parameter B is absent, sequence B is by default a sequence having members of 0s, then the comparison becomes one happening between sequence A and sequence [0…0] of the same lengh

cmp(r1,r2)

When comparing two records r1 and r2, return 0 if all field values of both are equal, otherwise compare field values in a normal order; for the first field values that are not equal, return 1 if the one in r1 is larger and return -1 if the one in A is smaller. If the number of fields in r1 and r2 are not the same and field values in the record that has less fields are completely equal to their corresponding ones in the other record, then the record with less fields will be smaller

Note:

The function compares two expressions x and y or two sequences A and B or two records r1 and r2. An error will be reported if x and y , A and B or r1 and r2 cannot be compared.

Parameter:

x/y

Expression

A/B

A sequence; can be a single value which is regarded as a sequence having one member

r1/ r2

A record

Example:

 

A

 

1

=cmp(9,5*1.2)

1.

2

=cmp([3,2,1],[1,8,9])

1.

3

=cmp([-1,4,8])

-1; equivalent to cmp([-1,4,8],[0,0,0]).

4

=cmp([0,3,5],0)

1; equivalent to cmp([0,3,5],[0,0,0]) .

5

=cmp(1000,"a")

Error is reported because a numeric value and a string cannot be compared.

6

=cmp("s","a")

1.

7

=cmp([0,3],[0,3,-5])

-1.

8

=demo.query("select EID,GENDER,SALARY from EMPLOYEE")

 

9

=demo.query("select EID,GENDER from EMPLOYEE")

 

10

=cmp(A8(1),A9(1))

1; as field values of A9 that has less fields are completely equal to their corresponding ones in A8, A9 is smaller.

11

=cmp(A8(1),A8(2))

-1.

12

=cmp([-1,-4,8,9],[-1,-4,8,9,10])

-1.

13

=cmp([-1,-4,8,9];[-1,-4,8,9,10])

-1; as the length of sequence A is less than that of sequence of B and members of the former are equal to their counterparts in the latter, the sequence with less members is smaller.

14

=cmp([-1,-4,8,9],[-1,-4,8])

1.

15

=cmp([-1,-4,8,9];[-1,-4,8])

0; because the length of sequence A is greater than that of sequence of B and members of the latter are equal to their counterparts in the former.

Related function:

Difference between sequences

Intersection of sequences

Union of sequences

Multiplication of sequences

Alignment Arithmetic Operations

Concatenation of sequences