bluebox
bluebox

Reputation: 555

How to compare the corresponding items of two arrays in a one-liner?

Say I have two arrays of the same length. E.g.:

Dim a() As Integer = {1, 2, 3, 4, 5}
Dim b() As Integer = {10, 20, 30, 40, 50}

Now I want to check if all items in b are greater than the corresponding items (same index) in a :

For i As Integer = 0 To a.Length - 1
    If b(i) <= a(i)
        Return False
    End If
Next
Return True

Is there a one-line solution for that? Maybe something using LINQ's All() method?

Upvotes: 0

Views: 70

Answers (2)

dbasnett
dbasnett

Reputation: 11773

In case the arrays are of different size

    Dim result As Boolean = a.Length = b.Length AndAlso Enumerable.Range(0, a.Length).All(Function(i As Integer) b(i) > a(i))

Upvotes: 1

Here's a one-liner for you:

Dim result As Boolean = Enumerable.Range(0, a.Length).All(Function(i As Integer) b(i) > a(i))

Upvotes: 2

Related Questions