[英]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
。
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完全匹配的咖啡名称应该在顶部,其余的可以是从服务器返回的顺序。
list.streams to use something like a LINQ list.streams使用类似LINQ的东西
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.