-1

why this code no sort by DESC? In SQL Browser the same query work but on android no. I need DESC list by records but this code not work

       val dbHelper = DataBaseHelper(applicationContext)
    val db = dbHelper.writableDatabase

    recyclerView.layoutManager=LinearLayoutManager(this)

    recyclerView.adapter = mojAdapter()

    val contentResolver = contentResolver
    val cursor = db.query(TableInfo.TABLE_NAME, arrayOf("records") ,null, null, null, null, "recordsDESC")

    try {

        cursor.moveToFirst()
        while (!cursor.isAfterLast){

            var name = cursor.getString(cursor.getColumnIndexOrThrow("records"))
            listakontaktow.add(name)
            cursor.moveToNext()
        }

    }

    finally {

        cursor.close()

    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jayeson
  • 1
  • 3
  • Possible duplicate of [Kotlin, recyclerview adapter how to sort results from query by DESC](https://stackoverflow.com/questions/53561869/kotlin-recyclerview-adapter-how-to-sort-results-from-query-by-desc) – forpas Dec 07 '18 at 15:51

1 Answers1

1

Using recordsDESC results in an attempt (very likely failed) to find the column called recordsDESC.

Add a space between records (the column name) and DESC (the keyword) to separate the column name from the keyword in the ORDER BY clause. The resultant Cursor will then have the rows sorted by the records column in descending order.

MikeT
  • 51,415
  • 16
  • 49
  • 68
ebasha
  • 422
  • 2
  • 4
  • Welcome to Stack Overflow. Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. [How to Answer](https://stackoverflow.com/help/how-to-answer). Thanks. – Elletlar Dec 07 '18 at 16:47