2

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.

3
  • there's a suggestion you can create a List of Pojo say List<Size> in Size put two-member sizeinCms,sizeinLetters, now you can sort it easily using sizeinCms as it will be an integer and will make your list sorted Commented May 26, 2017 at 10:34
  • But i'm not getting both sizesInCm and sizesInLetters. Either will be in cms or in letters. Commented May 26, 2017 at 10:36
  • 1
    A more maintainable solution might be to create an enum to store the sizes and their 'value' - a number which can be sorted without a custom comparator Commented May 26, 2017 at 10:44

1 Answer 1

8

Store sizes in right order in some list or map (for possible performance increase). Then you can create your own Comparator which will check position of searched size in that list/map and compare it to position of other element. All you need to do is compare these positions.

Something like:

private static Comparator<String> sizeComparator = new Comparator<String>() {

    private Map<String,Integer> sizes = new HashMap<>();
    {
        int i=0;
        for (String size : new String[]{"XS", "S", "M", "L", "XL", "XXL"}){
            sizes.put(size,i++);
        }
    }

    @Override
    public int compare(String o1, String o2) {
        return Integer.compare(sizes.get(o1),sizes.get(o2));
    }

};

Usage:

List<String> list = Arrays.asList("L", "M", "XL", "XS", "S", "XXL" );
System.out.println(list);
list.sort(sizeComparator);
System.out.println(list);

Output:

[L, M, XL, XS, S, XXL] //before sorting
[XS, S, M, L, XL, XXL] //after sorting
Sign up to request clarification or add additional context in comments.

1 Comment

Great solution! Thanks a million.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.