10

I need to compare an array of objects by one property of one of its objects property.
I am doing :

List<Sell> collect = sells.stream()
        .sorted(Comparator.comparing(Sell::getClient.name, String::compareToIgnoreCase))
        .collect(Collectors.toList());

It's not compiling, doesn anyone know how to do?

Thanks.

4
  • What is the error? What does Shell.getClient look like? Commented Nov 17, 2015 at 10:03
  • What is Sell, Client class definitions? What is sells of which you are using stream. Provide complete code. Commented Nov 17, 2015 at 10:08
  • If you want a case insensitive Comparator use String.CASE_INSENSITIVE_ORDER. String::compareToIgnoreCase is a comparison method returning an int not a Comparator Commented Nov 17, 2015 at 10:17
  • It just says it can't find getClient.name. In Sell object I have a Client object which has a property name. I have the feeling that I can't use this method with property of nested object. Commented Nov 17, 2015 at 11:04

3 Answers 3

8

This is the part of the code that causes an error

Sell::getClient.name

Your can create a reference to a (static or non-static) method of an arbitrary object of a particular type. A reference to the getClient method of any object of Sell type looks like this :

Sell::getClient

But method references are not objects and don't have members to access. With this code you are trying to access a member variable of the reference (and can't)

Sell::getClient.name

Also, method references are not classes so you can't get another method reference from them. You couldn't do something like that if you tried :

Sell::getClient::getName

Correct syntax for your particular case was provided by @mlk :

  1. x -> x.getClient().name
  2. Sell::getClientName (doesn't have to be a static method)
Sign up to request clarification or add additional context in comments.

Comments

3

In order to access the nested property and sort in reversed order, I am doing as:

Comparator<Sell> comparator = Comparator.comparing(h -> h.getAudit().getCreatedTs());
    sells.sort(comparator.reversed());

1 Comment

This was the only way to make it work for me: I've tried Sell.sort((Comparator.comparing(x -> x.getAudit().getCreatedTs()).reversed()); but kept having an error on the reversed method
1

I can't see your code, or the error you are getting. So this is a guess.

I believe you want

class Test {

    // I assume that Client looks like this.
    static class Client {
        public String name;
    }

    // And that Sell looks like this.
    // I'm sure your Client and Sell are bigger, but this is all 
    // that matters for now. 
    static class Sell {
        public Client getClient() { return null; }
    }

    public static void main (String[] args) throws java.lang.Exception
    {
        List<Sell> sells = new ArrayList<>();
        sells.stream().sorted(Comparator.comparing(x -> x.getClient().name, String::compareToIgnoreCase))
    }
}

You want to call a method on the return of that, so you need to create an anonymous function.

Your other option would be:

static class Sell {
    public String getClientName() { return null; }
}
// ...
        sells.stream().sorted(Comparator.comparing(Sell::getClientName, String::compareToIgnoreCase))

2 Comments

I agree with most of it. But if a method like getClientName is available, it doesn't have to be static. According to the tutorial you can get a "Reference to an instance method of an arbitrary object of a particular type"
Ah. OK fixed my anwser.

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.