The cleanest way would be:
Arrays.sort(months, Comparator.comparingInt(String::length));
or, with a static import:
Arrays.sort(months, comparingInt(String::length));
However, this would work too but is more verbose:
Arrays.sort(months,
(String a, String b) -> a.length() - b.length());
Or shorter:
Arrays.sort(months, (a, b) -> a.length() - b.length());
Finally your last one:
Arrays.sort(months,
(String a, String b) -> { return Integer.signum(a.length() - b.length()) };
);
has the ;
misplaced - it should be:
Arrays.sort(months,
(String a, String b) -> { return Integer.signum(a.length() - b.length()); }
);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…