0
{"SAMPLE:1-1-8-SAM#1", "SAMPLE:1-1-8-SAM#13","DEMO:1-1-4-SAM#13",DEMO:1-1-4-SAM#26,DEMO:1-1-4-SAM#8}

So, the output will be Alphabetical order and numeric also

DEMO:1-1-4-SAM#26
DEMO:1-1-4-SAM#13
DEMO:1-1-4-SAM#8
SAMPLE:1-1-8-SAM#1
SAMPLE:1-1-8-SAM#13

I have tried in ArrayList. The thing is, it is taking entire string as a string. But I want to sort string as well as number.Here what I got the output

DEMO:1-1-4-SAM#8
DEMO:1-1-4-SAM#13
DEMO:1-1-4-SAM#26
SAMPLE:1-1-8-SAM#1
SAMPLE:1-1-8-SAM#13
4
  • if the output on the top is desired output, how do you sort ascending and then having #26 in front of #13?? What exactly is the logic of the sort that you need? Commented Jan 28, 2015 at 12:29
  • ho, sorry, i didn't see that. but anyway, I think you got my point Commented Jan 28, 2015 at 12:33
  • Do you wish to sort all numbers or just the final number after the #? Commented Jan 28, 2015 at 12:37
  • all numbers and String! Commented Jan 28, 2015 at 17:33

2 Answers 2

4

You will have to write your own Comparator that splits up the strings into lexical parts and numerical parts and then compare them with the appropriate operator.

The following is a pseudo-code implementation of Comparator's only method compareTo to illustrate what I'm suggesting:

public void compare(String s1, String s2) {
  String[] e1 = split(s1);
  String[] e2 = split(s2);

  int n = Math.min(e1.length, e2.length);

  int ret = 0;

  for (int i = 0; ret == 0 && i < n; i++) {
    if (isNumber(e1[i])) {
      ret = compareNumerical(e1[i], e2[i]));
    } else {
      ret = compareLexical(e1[i], e2[i]));
    }
  }

  return ret;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Thomas, why not Comparator along with paralletSort from Java 8 API ? like, parallelSort(T[] a,Comparator<?super T> c)... it is much efficient because it assigns sorting tasks to multiple threads available in the thread pool.
@SufiyanGhori - This might be a good choice under the right circumstances but the overhead of Thread handling must be taken into account. For short arrays it's probably not worth it (?).
2

If you wish to sort the strings and consider all numbers you can use a combination of regex and Java 8.

Consider a string like the following:

"SAMPLE:1-2-8-SAM#1"

This can be split up using a regular expression to a String[] like this

String s = "SAMPLE:1-2-8-SAM#1";
String[] arr = s.split("((?<=[:\\-#])|(?=[:\\-#]))");
System.out.println(Arrays.stream(arr).collect(Collectors.toList()));

The output would be:

[SAMPLE, :, 1, -, 2, -, 8, -, SAM, #, 1]

This means that we can treat index 0 like a String and index 2, 4, 6 and 10 as Integers.

So, in order to use that for sorting the following code can be used:

String[] strings = {
        "SAMPLE:1-2-8-SAM#1",
        "SAMPLE:1-1-8-SAM#1",
        "SAMPLE:1-1-8-SAM#13",
        "DEMO:1-1-4-SAM#13",
        "DEMO:1-1-4-SAM#26",
        "DEMO:1-1-4-SAM#8"
};

final List<String> sorted = Arrays.stream(strings)
        .map(str -> str.split("((?<=[:\\-#])|(?=[:\\-#]))"))
        .sorted(Comparator
                .<String[], String>comparing((arr) -> arr[0])      // String compare
                .thenComparing(arr -> Integer.valueOf(arr[2]))     // Integer compare
                .thenComparing(arr -> Integer.valueOf(arr[4]))     // Integer compare
                .thenComparing(arr -> Integer.valueOf(arr[6]))     // Integer compare
                .thenComparing(arr -> Integer.valueOf(arr[10])))   // Integer compare
        .map(l -> Arrays.stream(l).collect(Collectors.joining()))  // Map it back to a String
        .collect(Collectors.toList());                             // Collect it to a List for further processing

It is not super-readable but it works as per the OP:s question. If you also use some static imports the code is a bit easier to read.


However, my recommendation is to create a Comparator that does the sorting which will be more readable (and thereby easier to maintain in the long run).

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.