I have array list with clothing sizes in this order: "L", "M", "XL", "XS", "S", "XXL" and i need to put in right order like this: "XS", "S", "M", "L", "XL", "XXL".
Collections.sort(arrayList);
will do the job if i have array with this sample: "38", "36", "34", "40", but not with the above. So i decided to do this with for loop and swapping items inside array list and put them on the right place, but because the size of array list is dynamically changed i can't know for sure which position should i assign.
I'm pretty sure that maybe this is not the right way to go, but i'm all open for suggestions how this can be solved.
Now here is one part of code:
// Sample clothing sizes
List<String> availableSizes = new ArrayList<>(tempSizes);
// Sorting array, only works for "34", "38", "36" and etc
Collections.sort(availableSizes);
if (availableSizes.size() > 0) {
for (int i = 0; i < availableSizes.size(); i++) {
if (availableSizes.get(i).equals("XS")) {
Collections.swap(availableSizes, i, 0);
} else if (availableSizes.get(i).equals("S")) {
Collections.swap(availableSizes, i, 1);
} else if (availableSizes.get(i).equals("M")) {
Collections.swap(availableSizes, i, 2);
} else if (availableSizes.get(i).equals("L")) {
Collections.swap(availableSizes, i, 3);
} else if (availableSizes.get(i).equals("XL")) {
Collections.swap(availableSizes, i, 4);
} else if (availableSizes.get(i).equals("XXL")) {
Collections.swap(availableSizes, i, 5);
}
}
}
Obviously i will get an error if array size is less than five.