How are the greater-than and less-than operators (`<`, `>`) interpreted when used between vectors?

Viewed 183

I tried the below variations:

vec![5, 5, 5] > vec![1, 1, 1] returns true
vec![5] > vec![1, 1, 1] returns true
vec![5, 0] > vec![6, 1] returns false
vec![1, 5, 2] > vec![0, 6, 1] returns true

It seems that only the first element is used to determine the output; vector size differences and the remaining elements are ignored from comparison. Is that how it is supposed to be?

Arrays seem to respond in the same way, the only difference is that both the arrays should be the same size.

1 Answers

That's called lexicographic order. Your tests are unfortunately misleading. vec![5, 5, 5] > vec![5, 5, 6] returns false, or in general:

vec![x, y] > vec![x, y - k]   // true for any k > 0 and (y - k) valid
vec![x, y, z] > vec![x, a, b] // same result as vec![y, z] > vec![a, b]

Lexicographic order is a common order for collection types. Note that Vec's Ord implementation also documents the comparison as lexicographic.

Related