简体   繁体   English

Java中的OrderByDecending(LINQ)等效项

[英]OrderByDecending(LINQ) equivalent in Java

Hi I am ac# developer getting into Java. 嗨,我是一名Java开发人员。 The question is simple: 问题很简单:

How can i write the below c# code into Java and still get it to work: 我如何才能将以下c#代码写入Java并仍能正常工作:

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

My sender.Text is basically a textbox.Text with a value like Cappuccino . 我的sender.Text是一个sender.Text ,其值类似于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. 好吧,我有一组咖啡,我希望与sender.text完全匹配的咖啡名称应该在顶部,其余的可以是从服务器返回的顺序。

I've checked out the below: 我已经检查了以下内容:

  1. list.streams to use something like a LINQ list.streams使用类似LINQ的东西

  2. Anotation type OrderBy but it's related to databases 注释类型OrderBy,但它与数据库有关

List<Coffee> sorted = lst.stream()
                .sorted(Comparator.comparing(s -> !s.trim().equalsIgnoreCase(sender.trim())))
                .collect(Collectors.toList());

Use Comparable and the sort method: 使用Comparable和sort方法:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM