Why not use classes?
As in Object-Oriented Principles, when you have data related to a specific topic, you put them in a class together.
Example:
NameDescription class implementing Comparable interface
public class NameDescription implements Comparable<NameDescription> {
private String name;
private String description;
public NameDescription(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int compareTo(NameDescription o) {
return name.compareTo(o.getName());
}
}
Comparable interface provides the compareTo method which makes you able to define the comparison formula to compare the current object with another one (they don't even need to be of the same type). Arrays.sort method uses the compareTo method to sort the elements, so this way you have your data together and any sorting method will give you consistent results based on your compareTo method.
Sorter class:
public class Sorter {
public static void main(String[] args) {
NameDescription namesDescriptions[] = {
new NameDescription("Drinks", "this is drinks package"),
new NameDescription("Bikes", "package for bikes"),
new NameDescription("Cars", "package cars"),
};
Arrays.sort(namesDescriptions);
// ASCENDING
for(int i=0; i<namesDescriptions.length; i++) {
System.out.println(namesDescriptions[i].getName());
}
for(int i=0; i<namesDescriptions.length; i++) {
System.out.println(namesDescriptions[i].getDescription());
}
System.out.println("---------");
// DESCENDING
for(int i=namesDescriptions.length-1; i>=0; i--) {
System.out.println(namesDescriptions[i].getName());
}
for(int i=namesDescriptions.length-1; i>=0; i--) {
System.out.println(namesDescriptions[i].getDescription());
}
// To save them in separate arrays
String[] names = new String[namesDescriptions.length];
String[] desc = new String[namesDescriptions.length];
String[] imageid = new String[namesDescriptions.length];
for(int i=0; i<namesDescriptions.length; i++) {
names[i] = namesDescriptions[namesDescriptions.length-i-1].getName();
desc[i] = namesDescriptions[namesDescriptions.length-i-1].getDescription();
imageid[i] = namesDescriptions[namesDescriptions.length-i-1].getimg();
}
}
}