My idea is to obtain all the attributes of a class from an instance of an object of that class, but when obtaining the collection of attributes, it stores them for me in alphabetical order and I want it to be returned to me in the order that they are defined in the builder.
This is my class:
class Empresa(override var id: Long = Long.MIN_VALUE, var uuid: String = "", var nombre : String = "")
And when doing:
var atributos = Empresa().javaClass.kotlin.memberProperties.toString()
I returned:
[id, nombre, uuid]
And the idea is that it returns as it is defined in the constructor (that it does not order them alphabetically):
[id, uuid, nombre]
Any ideas?
I think it's not possible. And it doesn't return them alphabetically, they just don't have a specific order, this case is a coincidence. You can see the java documentation
getDeclaredFields
, which clearly says:Translated:
edited
Apparently, despite what the documentation says,
getDeclaredFields
it does normally return the fields in declaration order (see this answer ). However, this is not 100% accurate, and it is probably best to disregard this order and check the field names.