1

Hi I am a c# developer getting into Java. The question is simple:

How can i write the below c# code into Java and still get it to work:

myCoffeeList.OrderByDescending(x => x.Name?.ToLower()?.Trim() == sender.Text.ToLower()?.Trim()));

My sender.Text is basically a textbox.Text with a value like Cappuccino.

What does the above code do?

Well I have a collection of coffee and I want that the name of the coffee that exactly matches the sender.text should be on top and the rest can be of the order returned from the server.

I've checked out the below:

  1. list.streams to use something like a LINQ

  2. Anotation type OrderBy but it's related to databases

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
iam.Carrot
  • 4,976
  • 2
  • 24
  • 71
  • The question context seems similar like this one: https://stackoverflow.com/questions/1217228/what-is-the-java-equivalent-for-linq. You can found something useful things there. – Tetsuya Yamamoto Nov 08 '17 at 09:25
  • You can use a custom `Comparable`, here is a good place to start learning about them https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html – Titus Nov 08 '17 at 09:28
  • @TetsuyaYamamoto No the question is quite different from that, reason being, I don't want to use LINQ. I just want to get an idea on how can I achieve what the above codeline does in Java – iam.Carrot Nov 08 '17 at 09:33
  • @Titus thanks I was looking at the `Comparable`, can you please help me out with a sample?\ – iam.Carrot Nov 08 '17 at 09:33
  • coffee.sort((a, b) -> a.equals(top) ? -1 : 0); look down – Stefan LoKran Dotti Nov 08 '17 at 09:34
  • GGO, I think the C# tag should stay, as there are a few recent/relatively advanced C# mechanisms at work here and I wouldn't expect a core java guru with passing knowledge of C# to reasonably know what all these things are.. – Caius Jard Nov 08 '17 at 09:36
  • @CaiusJard good suggest – iam.Carrot Nov 08 '17 at 09:37
  • @iam.Carrot unless I misunderstand the way `entity?.method` works, I don't think it's required on .ToLower() - ToLower() will never return null, and cannot be called on a null, so stopping at x.Name? would be fine => if name is null the runtime will cease trying to call further methods and evaluate the expression as null – Caius Jard Nov 08 '17 at 09:38
  • @CaiusJard Yeah thanks for that man. It just got as a habit to put that `?` maybe I over used it here. – iam.Carrot Nov 08 '17 at 09:54

2 Answers2

3
List<Coffee> sorted = lst.stream()
                .sorted(Comparator.comparing(s -> !s.trim().equalsIgnoreCase(sender.trim())))
                .collect(Collectors.toList());
J.R
  • 2,113
  • 19
  • 21
-1

Use Comparable and the sort method:

List<String> coffee = new ArrayList<>();
coffee.add("C");
coffee.add("A");
coffee.add("B");
coffee.add("D");

String top = "D";
coffee.sort((a, b) -> a.equals(top) ? -1 : 0);
coffee.forEach(System.out::println);

EDIT: for natural order use:

coffee.sort((a, b) -> a.equals(top) ? -1 : a.compareTo(b));