# Help me understand unintuitive behavior of array comparison

**URL:** <https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 25, 2021, 4:19pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946 "2021-03-25T16:19:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [March 25, 2021, 4:19pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/1 "2021-03-25T16:19:10Z")

</div>

```julia
julia> minimum([[1,1e10,1e10],[2,0,0]])
3-element Array{Float64,1}:
 1.0
 1.0e10
 1.0e10

```

I would expect this to return either the same as `min.()` or a `MethodError`, but instead only the first elements are compared. Similarly `[1,1e10,1e10]<[2,0,0]` is `true`, there is probably a good reason why but it’s hard to google for it.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 25, 2021, 4:22pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/2 "2021-03-25T16:22:06Z")

</div>

it’s comparing element by element (pair from both arrays) until it finds two elements that are not the same, and it will compare those two:

```julia
"""
    isless(A::AbstractVector, B::AbstractVector)

Returns true when `A` is less than `B` in lexicographic order.
"""
isless(A::AbstractVector, B::AbstractVector) = cmp(A, B) < 0

```

---

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [March 25, 2021, 7:16pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/3 "2021-03-25T19:16:07Z")

</div>

Thanks for the answer. I guess I understand _what_ it does, but not quite sure about the _why_. Is this standard in other languages? I can think of at least two other equally plausible ways to determine this comparison:

- which array’s sum is larger
- which array is larger in most element-wise comparisons

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 25, 2021, 7:18pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/4 "2021-03-25T19:18:11Z")

</div>

> [@levasco](#):
>
> Is this standard in other languages?

It is in python, at least.

> [@levasco](#):
>
> I can think of at least two other equally plausible ways to determine this comparison:

I think that is the _why_.

The option makes mostly sense for strings, arrays of characters, such things, which is how we organize things in dictionaries:

```julia
julia> ['B', 'A'] < ['C']
true

julia> ['B', 'A'] < ['A']
false

julia> "ba" < "c"
true

julia> "ba" < "a"
false

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 25, 2021, 7:26pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/5 "2021-03-25T19:26:29Z")

</div>

> [@lmiq](#):
>
> > [@levasco](#):
> >
> > Is this standard in other languages?
> 
> It is in python, at least.

Just for fun, I tried a few. Here’s Python:

```julia
>>> min([1,1e10,1e10], [2,0,0])
[1, 10000000000.0, 10000000000.0]

```

and Ruby:

```julia
irb(main):001:0> [[1,1e10,1e10], [2,0,0]].min
=> [1, 10000000000.0, 10000000000.0]

```

and C++:

```julia
julia> using Cxx

C++ > std::vector<std::vector<double>> values = {{1,1e10,1e10}, {2,0,0}}
true

C++ > auto min = *std::min_element(values.begin(), values.end())
true

C++ > min[0]
(double &) 1.0

C++ > min[1]
(double &) 1.0e10

C++ > min[2]
(double &) 1.0e10

```

These all agree with Julia because they’re all doing the same basic thing: comparing each vector in the list _lexicographically_.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [March 25, 2021, 7:26pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/6 "2021-03-25T19:26:54Z")

</div>

> [@levasco](#):
>
> Thanks for the answer. I guess I understand _what_ it does, but not quite sure about the _why_ . Is this standard in other languages?

Yes. It’s called lexicographic order.

---

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [March 25, 2021, 7:31pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/7 "2021-03-25T19:31:59Z")

</div>

Alright, thank you everyone for the responses! I never think about strings. Interesting that R is then the exception, as it does element-wise comparison by default, returning a boolean vector.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 25, 2021, 8:01pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/8 "2021-03-25T20:01:16Z")

</div>

> [@levasco](#):
>
> Interesting that R is then the exception, as it does element-wise comparison by default, returning a boolean vector.

This is because R has basically no concept of scalars, just vectors of unitary length. Consequently, element-wise operations are the default. Julia goes on the opposite direction, with a general broadcast operator that does element-wise operations when requested, so the default of every operation can be the non-element-wise interpretation (to be broadcasted on demand by just adding a single dot).

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 26, 2021, 1:00pm UTC](https://discourse.julialang.org/t/help-me-understand-unintuitive-behavior-of-array-comparison/57946/9 "2021-03-26T13:00:16Z")

</div>

> [@levasco](#):
>
> Is this standard in other languages?

Pretty much, but not may not be the relevant question. The important thing is that it is _documented_, see `?isless`.
