36

I want to sort String elements in the array months by length using Arrays.sort method. I was told here, that it's possible to use lambda expressions instead of creating new class implementing Comparator. Did it exactly the same way, yet it doesn't work.

import java.util.Arrays;
import java.util.Comparator;

public class MainClass {
public static void main(String[] args)
{

    String[] months = {"January","February","March","April","May","June","July","August","September","October","December"};

    System.out.println(Arrays.toString(months)); //printing before


    //neither this works:
    Arrays.sort(months, 
            (a, b) -> Integer.signum(a.length() - b.length())   
    );

    //nor this:
    Arrays.sort(months, 
            (String a, String b) -> { return Integer.signum(a.length() - b.length()) }; 
    );


    System.out.println(Arrays.toString(months)); //printing after
}
}
2
  • "Did it exactly the same way, yet it doesn't work." No you didn't, read carefully what they did. You have syntax errors. Commented Feb 23, 2014 at 16:11
  • @ZouZou Perhaps he did. I don't think the OP is even using Java 8. Commented Feb 23, 2014 at 16:21

4 Answers 4

89

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()); }
);
Sign up to request clarification or add additional context in comments.

11 Comments

+1 for showing Comparator.comparing, it is so much easier to grasp than writing a custom lambda or Comparator.
I suggest using Integer.compare instead of signum of -.
@PeterLawrey Indeed - signum(a-b) has the risk of overflowing (even if it's unlikely for strings).
@lavsprat huh.... Version 7 != Version 8... Java 8 is here: jdk8.java.net/download.html
I don't think - can overflow in this case, as both values are non-negative.
|
3

You're looking for this:

Arrays.sort(months, (a, b) -> Integer.signum(a.length() - b.length()));

4 Comments

Works for me... Are you sure you're using Java 8? Which IDE are you using?
@lavsprat It also works for me, it prints [May, June, July, March, April, August, January, October, February, December, September]... What Java 8 version are you using?
I'm using Eclipse. This site says I have Version 7 Update 51.
Well of course you can't use lambdas then, as they are only available in Java 8.
2

The functionality you are looking for is in Java 8, which has not yet been released. It is scheduled for release in a few months if you want to wait, or if not beta downloads are available.

4 Comments

Useful comment, but not an answer.
@TedHopp It is an answer. His code isn't compiling because he's trying to use a Java 8 feature in Java 7!
His code isn't compiling because it has a syntax error (semicolon at the end of the second argument to Arrays.sort).
This comment is a bit misleading, unless you count three weeks as being "a few months": ship date is March 18th
1

You can try this:

Arrays.sort(months, (a,b) -> Integer.compare(a.length(), b.length()));

1 Comment

Note that Integer#compare doesn't state it, but the return value is always -1, 0, or 1 (whereas the javadoc implies it's merely <0 or >0, so it is subject to change). #signum explicitly states this relationship. Either way, for Arrays#sort, I don't see what this answer contributes that the others didn't already cover 9 years ago.

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.