morozov
11/02/2019, 7:13 PMtarget?.let { targetList.sortedWith(compareBy {it.priority == 1})}
marstran
11/02/2019, 7:22 PMlet
?
If you want to sort the list in-place by priority, you can use targetList.sortBy { it.priority }
.morozov
11/02/2019, 7:29 PMtarget.let { targetList.sortBy { it.priority == 1 } }
in logs i see also all targetsmorozov
11/02/2019, 7:31 PMif (target?.priority == 1) targetList.add(target)
marstran
11/02/2019, 7:32 PMit.priority == 1
?morozov
11/02/2019, 7:36 PMmarstran
11/02/2019, 8:28 PMtargetList.sortByDescending { it.priority }
marstran
11/02/2019, 8:30 PMif (target?.priority == 1) {
targetList.sortByDescending()
}
marstran
11/02/2019, 8:30 PMmorozov
11/02/2019, 8:54 PMfun getTargetsByPriority() {
val valueEventListener = object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
targetList.clear()
for (targetSnapshot in dataSnapshot.children) {
val target = targetSnapshot.getValue(Target::class.java)
target?.let { targetList.add(it) }
}
targetList.sortedByDescending { it.priority }
contract.updateViewContent()
}
override fun onCancelled(databaseError: DatabaseError) {
Log.d("some", "Error trying to get targets for ${databaseError.message}")
}
}
targetsRef?.addListenerForSingleValueEvent(valueEventListener)
}
but get in log smth like this
2019-11-02 22:54:19.435 5489-5489/com.mandarine.target_list D/some: targetList: [Target(guid=-LrizWpFMU714rExtoit, name=huhy, description=hhhyy, date=0, priority=2), Target(guid=-LrizfGg0cKxlftLuL29, name=hhh, description=hhhh, date=0, priority=1), Target(guid=-Ls2Qr_H9jTbchvFBny4, name=hhh, description=hhhh, date=1572134400000, priority=0), Target(guid=-LshguMEgE6vPYMUxIQT, name=ооо, description=ооо, date=1574208000000, priority=1)]
morozov
11/02/2019, 8:54 PMmorozov
11/02/2019, 8:58 PMmorozov
11/02/2019, 8:58 PMmarstran
11/02/2019, 9:10 PMsortBy
sorts in-place, while sortedBy
returns a new sorted list.morozov
11/02/2019, 9:11 PMtargetList.sortByDescending { it.priority }
targetList.reverse()
marstran
11/02/2019, 10:04 PMtargetList.sortBy { it.priority }
. Why sort descending when you’re going to reverse it?morozov
11/03/2019, 7:04 PM