I have this project where I have a variable called "Floor" as a String. My task is to sort the floor asc and desc way to show on the Recycler View.
My Json response contains the following element:
"floor": 1
I have this options asc and desc inside a spinner so I will show you the swicth case correspondent:
case ASC:
//Sort array list into Ascending order.
Collections.sort(listModelsUa);
listModelsUa.add(modelUA);
case DESC:
Collections.reverse(listModelsUa);
listModelsUa.add(modelUA);
Note: My ModelUA implements Comparable and overrides the following method
String floor;
@Override
public int compareTo(@NonNull Object o) {
int compareTo=((ModelUA )o).getFloor();
/* For Ascending order*/
return this.floor-compareTo;
}
My question is,there are any other way to sort values inside an ArrayList using strings(maybe cast to Int) and displaying in the Recycler view?
Integer.parseInt(text);. Then you can sort them like integer.ModelUAimplementComparableinstead ofComparable<ModelUA>? Or, why do you haveObjectin yourcompareTomethod? Use generics, not raw types.