There are valid variables int ABC = 10, int BAC = 9, CBA = 8. I want to display them on the android as a list and be able to sort by values ​​and sort the variables alphabetically. What are the opportunities for this?

UPD

public static Comparator<ModelData> Comparator_name = new Comparator<ModelData>() { @Override public int compare(ModelData o1, ModelData o2) { return 0; } }; 
  • It is possible through reflection to get the name of the field, take the Map <String, Integer>, write 2 comparators. Depending on the need to use the right. Satisfy the interest, why do you need it, if not for fun? - Victor
  • Well, I believe that there is no difficulty in displaying on the screen. Sort by value is a trivial thing. Add them to the array or to the collection, then Arrays.sort Collections.sort. If you want to sort by variable names, meaning ABC, BAC, CBA, then uvas one way - reflection. The class has a class.getFields method, it will return a list of all fields, if the fields are private, then getDeclaredFields. When you get the names, you take their value and, again, sort them in the old way. But this is a bad decision. The naming of variables relates to internal logic. Expecting them to draw a UI is a clear architecture error - Dmitry
  • There is a list of currencies, values ​​are stored in them. So sort them alphabetically is just as important as by value. - Turalllb
  • There is also a question with mapping. After all, I want the list to display both the name and value. The variant of Victor seems to be suitable, but I don’t know how to write a comporator. Have to read. And I also read that for android there is an Arraymap is an alternative to hashmap and in java there is none, it was made specifically for android - Turalllb

1 answer 1

For your task, you do not need to operate with variable names, although this is possible with the help of reflection.

Reflection can break if you apply code obfuscation, which changes the variable names. You need another way.

You have a data model with currency and price values. So you need to create a class for storing this data with comparators and equals&hashCode something like this:

 public class MyData { public String name; public BigInteger value; public MyData(String name, BigInteger value) { this.name = name; this.value = value; } public static Comparator<MyData> COMPARATOR_NAME = (o1, o2) -> o1.name.compareTo(o2.name); public static Comparator<MyData> COMPARATOR_VALUE = (o1, o2) -> o1.value.compareTo(o2.value); public int hashCode() { return name.hashCode() + value.hashCode(); } public boolean equals(Object other) { if (this === other) return true; if (getClass() != other.getClass()) return false; MyData otherData = (MyData) other; if (name.equals(otherData.name)) return false; if (value != otherData.value) return false; return true; } } 

Now you can use this model in adapters and sort as you like by any field. And no reflection is necessary.

  • The maximum that I understood this idea. I decided to stupidly copy and try. but this (o1, o2) -> o1.name.compareTo (o2.name) is underlined in red and writes that lambda expressions are not supported at this language level. Does Android SDK already support Java 8? - Turalllb
  • @Turalllb, some features of 8 Java are supported. Lambdas can be used either via the retrolamba plugin or studio 3, grad 3, targetSdk 26 and SourceCompatibility 1.8 in build.gradle - YuriiSPb
  • All this is now difficult, and it would be necessary for it to work normally on early versions as well. And I don’t even realize this line underline)) - Turalllb
  • Well ... You can write about the old Comparator in the old way and everything will be replaced by the autodog - YuriySPb
  • added to the question what happened with autocompletion. Is this equivalent to what you wrote? Explain, please, what this line does in general, what an arrow, what values ​​o1, o2. How does this line compare? - Turalllb