Here’s an alternative method in Java that utilizes a custom Comparator: import java . util . * ; public class Main { public static void main ( String [ ] args ) { String [ ] strings = { "10" , "1" , "20" , "b" , "a" } ; Arrays . sort ( strings , new Comparator < String > ( ) { public int compare ( String a , String b ) { try { return Integer . compare ( Integer . parseInt ( a ) , Integer . parseInt ( b ) ) ; } catch ( NumberFormatException e ) { return a . compareTo ( b ) ; } } } ) ; for ( String s : strings ) { System . out . println ( s ) ; } } } This Java program creates a Comparator that attempts to parse the strings as integers for comparison. If the parsing is successful, it compares them as integers; otherwise, it defaults to lexicographical comparison.