0

I have a list of objects and I want to make alphanumeric asc sorting by name. I'm using SAP Hybris and java 11.

   public class TestSort{
 
    private String name;
    private String description;
    private String lenghtDimension;
    private String widthDimension;
    private String thicknessDimension;
    //code
   }

    List<TestSort> objects = new ArrayList<>();
   

    Collections.sort(getAllobjects(), (O1,O2) -> O1.getName().compareTo(O2.getName()));

But I have the following result

   "GGGG F B 12.5 x 1200 x 2400 S"
   "GGGG F B 12.5 x 1200 x 2400 T"
   "GGGG F B 12.5 x 1200 x 2700 T"
   "GGGG F B 12.5 x 1200 x 3000 T"
   "GGGG F B 12.5 x 900 x 1800 T"
   "GGGG F B 15 x 1200 x 2400 T"
   "GGGG F B 15 x 1200 x 2700 T"
   "GGGG F B 15 x 900 x 1800 T"

And the results I should have is :

   "GGGG F B 12.5 x 900 x 1800 T"
   "GGGG F B 12.5 x 1200 x 2400 S"
   "GGGG F B 12.5 x 1200 x 2400 T"
   "GGGG F B 12.5 x 1200 x 2700 T"
   "GGGG F B 12.5 x 1200 x 3000 T"
   "GGGG F B 15 x 900 x 1800 T"
   "GGGG F B 15 x 1200 x 2400 T"
   "GGGG F B 15 x 1200 x 2700 T"

I need a generic solution because the name can be in another format.

UPDATE :

The name contain 15 x 1200 x 2700 , lenghtDimension x widthDimension X thicknessDimension

6
  • 2
    Why do you expect 900 x 1800 to come before 1200 x 3000? The string-sorting is lexicographical and 1 comes before 9 in the alphabet. Thats why it sorts like that. Do you actually want to interpret the numbers as numbers and sort them ascending instead of lexicographical? This will be quite difficult if you only have the data as text, since you will have to extract the numbers and parse them. Commented Jul 29, 2021 at 11:22
  • You're going to have to break the dimension string into pieces to be able to sort it by dimension. Commented Jul 29, 2021 at 11:23
  • @Zabuzard, I updated my code I modified my code, my model contains the dimensions And the name is made up of name and dimensions Commented Jul 29, 2021 at 11:31
  • You will have to split the string into its individual sections, parse them as actual numbers and go for a much more advanced Comparator that combines all of that. Commented Jul 29, 2021 at 11:40
  • As first step, do you actually have to take all of the data as String? On the level of your class, cant you have it as double length; int width; int thickness;? Would be much easier then. Commented Jul 29, 2021 at 11:42

1 Answer 1

4

You have to write a custom comparator using a regular expressions:

public static class Data {

    private final String name;
    private final String description;

    public Data(String name, String description) {
        this.name = name;
        this.description = description;
    }

}

public static void main(String... args) {
    List<Data> data = new ArrayList<>();
    data.add(new Data("GGGG F B 15 x 1200 x 2700 T", "Description"));
    data.add(new Data("GGGG F B 15 x 900 x 1800 T", "Description"));
    data.add(new Data("GGGG F B 12.5 x 1200 x 2400 T", "Description"));
    data.add(new Data("GGGG F B 12.5 x 1200 x 3000 T", "Description"));
    data.add(new Data("GGGG F B 12.5 x 900 x 1800 T", "Description"));
    data.add(new Data("GGGG F B 12.5 x 1200 x 2400 S", "Description"));
    data.add(new Data("GGGG F B 15 x 1200 x 2400 T", "Description"));
    data.add(new Data("GGGG F B 12.5 x 1200 x 2700 T", "Description"));
    data.add(new Data("GGGG F B 15 x 900 x 1800 T", "Description"));

    Comparator<Data> sortByName = new Comparator<>() {
        private final Pattern pattern = Pattern.compile("([^\\s])+\\s+([^\\s])+\\s+" +
                "([^\\s])+\\s+(?<length>[^\\s]+)\\sx\\s+(?<width>[^\\s]+)\\s+" +
                "x\\s+(?<thickness>[^\\s]+)\\s+([^\\s]+)");

        @Override
        public int compare(Data one, Data two) {
            Matcher matcherOne = pattern.matcher(one.name);
            Matcher matcherTwo = pattern.matcher(two.name);
            boolean matchOne = matcherOne.matches();
            boolean matchTwo = matcherTwo.matches();

            if (!matchOne || !matchTwo)
                return 0;

            int res = compareString(matcherOne, matcherTwo, 1);
            res = res != 0 ? res : compareString(matcherOne, matcherTwo, 2);
            res = res != 0 ? res : compareString(matcherOne, matcherTwo, 3);
            res = res != 0 ? res : compareDouble(matcherOne, matcherTwo, "length");
            res = res != 0 ? res : compareDouble(matcherOne, matcherTwo, "width");
            res = res != 0 ? res : compareDouble(matcherOne, matcherTwo, "thickness");
            res = res != 0 ? res : compareString(matcherOne, matcherTwo, 7);

            return res;
        }

        private int compareDouble(Matcher one, Matcher two, String group) {
            double a = Double.parseDouble(one.group(group));
            double b = Double.parseDouble(two.group(group));
            return Double.compare(a, b);
        }

        private int compareString(Matcher one, Matcher two, int group) {
            String a = one.group(group);
            String b = two.group(group);
            return a.compareTo(b);
        }
    };

    data.forEach(d -> System.out.println(d.name));
    data.sort(sortByName);
    System.out.println();
    data.forEach(d -> System.out.println(d.name));
}

Output

GGGG F B 15 x 1200 x 2700 T
GGGG F B 15 x 900 x 1800 T
GGGG F B 12.5 x 1200 x 2400 T
GGGG F B 12.5 x 1200 x 3000 T
GGGG F B 12.5 x 900 x 1800 T
GGGG F B 12.5 x 1200 x 2400 S
GGGG F B 15 x 1200 x 2400 T
GGGG F B 12.5 x 1200 x 2700 T
GGGG F B 15 x 900 x 1800 T

GGGG F B 12.5 x 900 x 1800 T
GGGG F B 12.5 x 1200 x 2400 S
GGGG F B 12.5 x 1200 x 2400 T
GGGG F B 12.5 x 1200 x 2700 T
GGGG F B 12.5 x 1200 x 3000 T
GGGG F B 15 x 900 x 1800 T
GGGG F B 15 x 900 x 1800 T
GGGG F B 15 x 1200 x 2400 T
GGGG F B 15 x 1200 x 2700 T
Sign up to request clarification or add additional context in comments.

Comments

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.