In Java 8:
Object[] myArray = {(String) "U", (String) "U", (int) 2, (String) "X", (int) 4, (String) "U"};
Stream.of(myArray).filter(x -> !(x instanceof String))
.sorted().forEach(System.out::print);
Stream.of(myArray).filter(x -> x instanceof String)
.sorted().forEach(System.out::print);
For David Wallace: in case you want to save the sorted array (I save it to List in this example but it can be converted into .toArray() if you want):
Object[] myArray = {(String) "U", (String) "U", (int) 2, (String) "X", (int) 4, (String) "U"};
List<Object> newList = Stream.of(myArray).filter(x -> x instanceof String)
.sorted().collect(Collectors.toList());
Collections.addAll(newList, Stream.of(myArray).filter(x -> !(x instanceof String))
.sorted().toArray());
for (Object o : newList) {
System.out.print(o);
}
OUTPUT (of both code snippets):
24UUUX
That said, it's a bad practice to mix different types in the same array (to use Object like you did). Different types should be "mixed" only if they have an interface in common (or, if one of them extends the other )!
Stringand integer literals toint?