How can I sort by a method that returns Boolean in...
# announcements
j
How can I sort by a method that returns Boolean in Kotlin? I was using
Comparator
which returns an
Int
but now I need more complex stuff, so I have a method that returns
True
or
False
depending of a check, is there any way I'd use this method to sort it? Is getting
Foo1
Foo2
as a parameter and then do the comparation.
d
You can use the
compareBy
helper probably, which creates a comparator. Example:
val comparator = compareBy<String> { it[0].isUpperCase() }
This is a Comparator, which sorts Strings by whether they start with an upper case letter or not.
compareBy
can also take multiple lambdas and will check them in order (if first one compares them as equal it will check the 2nd one and so on)
j
doing it[0] and it[1] I'm comparing 2 Strings then?
d
No, with
compareBy
you are not explaining how to compare, you explaining what to compare. In this case I am saying "compare by whether or not the first character (
it[0]
) is upper case".
j
Can you check my second message?
That's what I have and I'd like to sort depending of it
d
Well, a boolean is not enough to determine whether an element is greater or smaller than another
Given two elements, you need to determine which of the two is larger
You can't do that with just a boolean
3 cases: both equal first larger second larger
j
But if I'm checking 2 different values how I'm supposed to retutnr equal first or second? Like this :
Copy code
else {
            (foo1.first()
                    || (foo1.second() && !foo2.third())
                    || (foo1.fourth() && foo2.fifth()))
        }
d
I don't know, you need to decide on which order you want. If that is not clear, you can't start implementing a comparator.
☝️ 1
And an order always means you can clearly decide which element of two is bigger
j
I know the order
Is that one I sent to you
d
I don't see an order...?
An order needs to return three possible values: • a is greater • b is greater • a == b
You cannot do that by returning a boolean.
j
But why I can not use Boolean.compare(something==somethingelse)
as a return?
d
You can. But it returns an Int, not a Boolean.
j
Yes, but comparator needs an int
So perhaps I can do this :S
d
Yes, that's what I have been saying all along...
j
Why I can not use Boolean.compareTo()?
is in red
d
firstBoolean.compareTo(otherBoolean)
j
No, I mean can I return this?
Copy code
foo1.endDate <= foo2.endDate
Converting to an int?
d
No, that's a
Boolean
again, not an
Int
.
It doesn't make sense to convert that to an int. The int needs to be one of -1, 0 and 1. A boolean is just true and false...
If you want to sort by end date you need to do
foo1.endDate.compareTo(foo2.endDate)
OR you can use the compareBy helper:
compareBy { it.endDate }
Which is equivalent.
j
I have a List<Foo>
And I want to use the compareBy there
d
Either:
Copy code
val comparator = compareBy<Foo> { it.endDate }
list.sortedWith(comparator)
Or:
Copy code
list.sortedBy { it.endDate }
Same thing
j
And then can I do more operations inside of
sortedBy
d
You can do however much you want in there, yes. In the end it needs to produce something comparable. The list is then sorted by whatever you produce there. If you just do
{ it.endDate }
it just sorts by end date. But you can do complex calculations as well, it will then sort by those.
j
But that's the thing I can only sort checking one it, I want to compare it with other one like I'm doing on my comparator check
Copy code
object EndCompi : Comparator<Foo> {
        override fun compare(foo1: Foo, foo2: Foo): Int {
            return foo1.endDate.compareTo(foo2.endDate)
        }
    }
Then calling
it.sortedWith(EndCompi)
d
Okay, then do that...? I am not sure what your question is...
j
Thing is, if I have to do something like this :
Copy code
(foo1.first()
                    || (foo1.second() && !foo2.third())
                    || (foo1.fourth() && foo2.fifth()))
        }
I'm lost because sortedBy can only check the current it
Can not check for it1.first() or it1.second && !it2.third
d
Then use a comparator... And not the
sortedBy
helper.
Then you get two values, but you need to be explicit about the result (-1 => foo1 is smaller, 0 => they are equal, 1 => foo2 is smaller)
j
Ok... let me check
922 Views