Joan Colmenero
12/18/2019, 12:50 PMComparator
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.diesieben07
12/18/2019, 12:55 PMcompareBy
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)Joan Colmenero
12/18/2019, 12:57 PMdiesieben07
12/18/2019, 12:58 PMcompareBy
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".Joan Colmenero
12/18/2019, 12:58 PMJoan Colmenero
12/18/2019, 12:59 PMdiesieben07
12/18/2019, 12:59 PMdiesieben07
12/18/2019, 1:00 PMdiesieben07
12/18/2019, 1:00 PMdiesieben07
12/18/2019, 1:00 PMJoan Colmenero
12/18/2019, 1:13 PMelse {
(foo1.first()
|| (foo1.second() && !foo2.third())
|| (foo1.fourth() && foo2.fifth()))
}
diesieben07
12/18/2019, 1:15 PMdiesieben07
12/18/2019, 1:15 PMJoan Colmenero
12/18/2019, 1:37 PMJoan Colmenero
12/18/2019, 1:37 PMdiesieben07
12/18/2019, 1:37 PMdiesieben07
12/18/2019, 1:41 PMdiesieben07
12/18/2019, 1:41 PMJoan Colmenero
12/18/2019, 1:52 PMJoan Colmenero
12/18/2019, 1:52 PMdiesieben07
12/18/2019, 1:52 PMJoan Colmenero
12/18/2019, 1:53 PMJoan Colmenero
12/18/2019, 1:53 PMdiesieben07
12/18/2019, 1:53 PMJoan Colmenero
12/18/2019, 1:54 PMJoan Colmenero
12/18/2019, 1:54 PMdiesieben07
12/18/2019, 1:54 PMfirstBoolean.compareTo(otherBoolean)
Joan Colmenero
12/18/2019, 1:55 PMfoo1.endDate <= foo2.endDate
Joan Colmenero
12/18/2019, 1:55 PMdiesieben07
12/18/2019, 1:55 PMBoolean
again, not an Int
.diesieben07
12/18/2019, 1:56 PMdiesieben07
12/18/2019, 1:56 PMfoo1.endDate.compareTo(foo2.endDate)
diesieben07
12/18/2019, 1:56 PMcompareBy { it.endDate }
diesieben07
12/18/2019, 1:56 PMJoan Colmenero
12/18/2019, 1:57 PMJoan Colmenero
12/18/2019, 1:58 PMdiesieben07
12/18/2019, 1:58 PMval comparator = compareBy<Foo> { it.endDate }
list.sortedWith(comparator)
Or:
list.sortedBy { it.endDate }
Same thingJoan Colmenero
12/18/2019, 1:59 PMsortedBy
diesieben07
12/18/2019, 2:00 PM{ it.endDate }
it just sorts by end date. But you can do complex calculations as well, it will then sort by those.Joan Colmenero
12/18/2019, 2:00 PMobject EndCompi : Comparator<Foo> {
override fun compare(foo1: Foo, foo2: Foo): Int {
return foo1.endDate.compareTo(foo2.endDate)
}
}
Then calling it.sortedWith(EndCompi)
diesieben07
12/18/2019, 2:01 PMJoan Colmenero
12/18/2019, 2:01 PM(foo1.first()
|| (foo1.second() && !foo2.third())
|| (foo1.fourth() && foo2.fifth()))
}
Joan Colmenero
12/18/2019, 2:01 PMJoan Colmenero
12/18/2019, 2:02 PMdiesieben07
12/18/2019, 2:02 PMsortedBy
helper.diesieben07
12/18/2019, 2:03 PMJoan Colmenero
12/18/2019, 2:03 PM