-1

When a String object is given as follows, it should split these values into pieces, sort them with “, ve and return a List by grouping the numbers.

String=123456 4568 567852 78212.





island
  • 43
  • 1
  • 11
  • 1
    Create a `class`. Java is an OO language. That `class` can hold some named parts of the token - whatever they might be. Now you simply need a `Comparator` that understands your `class`. You could even simply `implements Comparable<...>`. – Boris the Spider Nov 12 '19 at 07:03
  • There's plenty of tutorials online on how to implement comparator. You posted your comment saying you cannot do it literally 7 minutes after reading Boris's comment. That's clearly not enough effort on your part in solving your problem. Try to modify your code to implement comparator successfully, by implementing basic comparison logic even if it doesn't necessarily give you the output you want. Then attempt to modify your logic to get your desired output. – John Kim Nov 12 '19 at 07:23
  • You didn't do enough research. If you got errors then solve the errors. I'm not trying to grill you, but this is clearly a homework problem. You need to learn to solve such problems on your own instead of asking questions on SO for solutions that are easily researchable. I spent 3 minutes researching for a solution and implemented a working comparator that is on the right track. Here it is : https://pastebin.com/FZK4FzKj Note it doesn't give you the output you want, but it's a starter solution using comparator. – John Kim Nov 12 '19 at 07:37
  • Added as answer. I feel as if I'm giving away too much here, but to be fair you could have found this solution easily somewhere else, and I'm guessing English isn't your first language and that's the reason you are having trouble researching for a solution. – John Kim Nov 12 '19 at 07:55
  • Possible duplicate of [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – island Nov 18 '19 at 06:03

1 Answers1

0

Using a comparator you can try this :

public static void main(String[] args) {
    String input = "11B,11A,12,12/1,13,14,15,16,16A,17,17/1,57-51B,57,57-51A,1,10,11,53B,53A,53C,57-51C,57A";
    List<String> items = Arrays.asList(input.split("\\s*,\\s*"));


    Collections.sort(items, new Comparator<String>() {
        public int compare(String o1, String o2) {

            String o1StringPart = o1.replaceAll("\\d", "");
            String o2StringPart = o2.replaceAll("\\d", "");


            if(o1StringPart.equalsIgnoreCase(o2StringPart))
            {
                return extractInt(o1) - extractInt(o2);
            }
            return o1.compareTo(o2);
        }

        int extractInt(String s) {
            String num = s.replaceAll("\\D", "");
            // return 0 if no digits found
            return num.isEmpty() ? 0 : Integer.parseInt(num);
        }
    });

    for (String s : items) {
        System.out.println(s);
    }
}

This won't do exactly what you want in terms of desired output but it should get you started.

John Kim
  • 1,081
  • 10
  • 26
  • Note that as the comparator can be called `log(n)` times, doing work in the comparator is usually a bad idea. Although the answer does illustrate how to begin to approach the problem. – Boris the Spider Nov 12 '19 at 08:13
  • Thanks for your help.@BoristheSpider – island Nov 12 '19 at 09:40
  • @BoristheSpider that’s optimistic ;) The comparator will be invoked *at least* `n - 1` times, that’s unavoidable. The worst case is on a scale of `n` times `log n`… – Holger Nov 14 '19 at 16:49
  • @Holger I meant per item - which I think is correct? As in, the cost of the tokenisation n times cannot be avoided but by doing it in the comparator it will be done multiple times per item. – Boris the Spider Nov 14 '19 at 16:51
  • 1
    @BoristheSpider the worst case is on the scale of `O(n log n)` (mind that this is not an approximation of the actual number of invocations), but since the comparator performs two tokenizations on each invocation, even in the best case of `n - 1` invocations, it’s doing twice the necessary work. Still, I’d keep doing it on-the-fly, but eliminate the tokenization, as instead of a heavy `replaceAll(regex)`, the comparison can be done with a simple character iteration only slightly heavier than, e.g. `String.CASE_INSENSITIVE_ORDER` – Holger Nov 14 '19 at 17:28