I have a simple class which is auto-generated and use to store responses from Retrofit. Anyway in the final step I would like to use row content, sort every element by position from highest to lowest and after sorting convert it to String[] with name only. How can I do that in the most efficient way?
public class RowModel implements Comparable<RowModel>{
private String name;
private double position;
public double getPosition() {
return position;
}
public void setPosition(float position) {
this.position = position;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public RowModel(String name, double position) {
this.name = name;
this.position = position;
}
}
After my search online I found this method to execute sorting but I'm not sure that the results are correct. And of course I still don't know how to convert sorted names to String[] at the final step:
@Override
public int compareTo(RowModel rowModel) {
double comparePosition = ((RowModel) rowModel).getPosition();
return (int) (this.position- comparePosition);
}
Collections.sort( List<RowModel>... )to sort, and there is more than one way to getString[]of names one of which is you could create an array of Strings and traverse the list and fill array withit.next().getName()