tried smth like this ``` target?.let { targetList....
# getting-started
m
tried smth like this
Copy code
target?.let { targetList.sortedWith(compareBy {it.priority == 1})}
m
Why do you need to call to
let
? If you want to sort the list in-place by priority, you can use
targetList.sortBy { it.priority }
.
m
if i write smth like this
Copy code
target.let { targetList.sortBy { it.priority == 1 } }
in logs i see also all targets
can write smth like this
Copy code
if (target?.priority == 1) targetList.add(target)
m
I have no idea what you’re trying to do. Why would you want to sort by
it.priority == 1
?
m
I want to sort my list by desceding now i sort my list if target priority is 1
m
There’s
targetList.sortByDescending { it.priority }
Or is this what you need?
Copy code
if (target?.priority == 1) {
    targetList.sortByDescending()
}
I’m still confused 😛
m
i write smth like this now
Copy code
fun 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
Copy code
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)]
priority not sorted(
fixed! changed on sordBy not sorted
thx)
m
Yes,
sortBy
sorts in-place, while
sortedBy
returns a new sorted list.
m
but can i simplify this method?
Copy code
targetList.sortByDescending { it.priority }
targetList.reverse()
m
Just do
targetList.sortBy { it.priority }
. Why sort descending when you’re going to reverse it?
m
yep, u are right, thx)