java - How to reverse a String array based on it's index value -
i want reverse this:
customarray = myarray; namearray = new string[myarray.size()]; (int = 0; < myarray.size(); i++) { idarray[i] = myarray.get(i).getunitid(); namearray[i] = myarray.get(i).getunitname(); } if (sorttype == sort) arrays.sort(idarray, collections.reverseorder()); arrays.sort(namearray, ...);
i know how reverse using arrays.sort(stringarray, collections.reverseorder());
not index value.
how can reverse order of namearray based on it's namearray[i]
?.. or better, since idarray list of unique id's sort namearray based on idarray.
the solution problem use oop approach. after all, java oop language. instead of having 2 arrays:
int[] unitid string[] unitname
which can't sort in way indexes stay corresponding, write class unit
implements comparable<unit>
, use 1 array:
unit[] units
then
arrays.sort(units);
will job.
you need own class:
public class unit implements comparable<unit> { private int id; private string name; // constructor // methods @override public int compareto(unit other) { // sorts id return this.id - other.id; // sort name, use this: // return this.name.compareto(other.name); } }
Comments
Post a Comment