I have 2 String[] Arrays.
the first is itemArray, it contains the names of products.
the second is priceArray, it contains the prices for each item.
itemArray[0] relates to priceArray[0]
itemArray[1] relates to priceArray[1]
I need to sort the arrays based on the price.
Using the following code will order the price array
List<Float> pricefloatArrayTo Order = new ArrayList<Float>();
for (String s : priceArray) {
pricefloatArrayTo (Float.valueOf(s));
}
Collections.sort(pricefloatArrayTo );
Only problem is, the itemArray now needs ordering in accordance with the priceArray so that the arrays once again match up.
Can anone suggest a method that I can use to sort both arrays based on the priceArray. Thanks in advance
How about instead of keeping the elements separated in two arrays, create a new class which contains both the Price and the Item. You can them make this class implement the Comparable interface which you can use to sort an array containing this new class by Price.
For example:
(I’ve removed the normal get/set encapsulation and null checks for clarity)
You can then have an array of this class(or a List) which you can then sort.