Create an object that will hold both the index and the text string. Like
public class MyThing
{
public int index;
public String text;
}
Then instead of creating an ArrayList of Strings, create an ArrayList of these objects.
I don't know what you're using to sort them. If you're writing your own sort, than you can simply do your comparisons against the "text" member of each object rather than against the string object itself. If you're using, say, Arrays.sort to sort it, then you need to implement Comparable. Namely:
public class MyThing implements Comparable<MyThing>
{
public int index;
public String text;
public int compareTo(MyThing that)
{
return this.text.compareTo(that.text);
// May need to be more complex if you need to handle nulls, etc
}
// If you implement compareTo you should override equals ...
public boolean equals(Object that)
{
if (!(that instanceof MyThing))
{
return false;
}
else
{
MyThing thatThing=(MyThing)that;
return this.text.equals(thatThing.text);
}
}
}
Etc. You may need other stuff depending on what you're trying to do.