This is not like Collections.sort() where the parameter reference gets sorted. In this case you just get a sorted stream that you need to collect and assign to another variable eventually:
CopyList result = list.stream().sorted((o1, o2)->o1.getItem().getValue().
compareTo(o2.getItem().getValue())).
collect(Collectors.toList());
You've just missed to assign the result
Answer from Jan B. on Stack OverflowVideos
This is not like Collections.sort() where the parameter reference gets sorted. In this case you just get a sorted stream that you need to collect and assign to another variable eventually:
CopyList result = list.stream().sorted((o1, o2)->o1.getItem().getValue().
compareTo(o2.getItem().getValue())).
collect(Collectors.toList());
You've just missed to assign the result
Use list.sort instead:
Copylist.sort((o1, o2) -> o1.getItem().getValue().compareTo(o2.getItem().getValue()));
and make it more succinct using Comparator.comparing:
Copylist.sort(Comparator.comparing(o -> o.getItem().getValue()));
After either of these, list itself will be sorted.
Your issue is that
list.stream.sorted returns the sorted data, it doesn't sort in place as you're expecting.
Good morning everyone!
I was making the code for this method called ordersWithStatus(). The method should return informations about the orders. Such method returns a String obtained by concatenating all orders satisfying the criteria:
Only orders with a given status passed by argument to the method
The orders have to be sorted by: restaurant name, customer's first name, customer's last name, delivery time.
I don't understand why the method using the stream (the commented one) doesn't work, but the other one using Collections.sort() works. Expected: Napoli, Judi Dench : (19:00): M6->1 Napoli, Ralph Fiennes : (19:00): M1->2 M6->1 But was: Napoli, Ralph Fiennes : (19:00): M1->2 M6->1 Napoli, Judi Dench : (19:00): M6->1 I noted that using the stream, it seems that the method doesn't sort the output, it prints Ralph Fiennes first and then Judi Dench. What am I doing wrong with the stream? Could you please help me ?
Here is my code:
/**
* Retrieve all order with a given status with all the relative details in text format.
* The list is sorted by name of restaurant, name of the customer, and delivery time.
* @param status the status to be matched
* @return textual representation of orders
*/
public String ordersWithStatus(OrderStatus status) {
/*
Set<String> outputSet = this.orders.stream()
.filter(o->o.getStatus()==status)
.sorted( Comparator.comparing(Order::getRestaurantName)
.thenComparing(Order::getCustomerName1)
.thenComparing(Order::getCustomerName2)
.thenComparing(Order::getTime ))
.map(Order::toString)
.collect( Collectors.toSet() );
String output = "";
for (String order : outputSet){
output+=order;
}
return output;
*/
// Pulling out each comparision step into a local reference
Comparator<Order> byRestaurantName = (o1, o2) -> o1.getRestaurantName().
compareTo(o2.getRestaurantName());
Comparator<Order> byCustomer1name = (o1, o2) -> o1.getCustomerName1().
compareTo(o2.getCustomerName1());
Comparator<Order> byCustomer2name = (o1, o2) -> o1.getCustomerName2().
compareTo(o2.getCustomerName2());
Comparator<Order> byTime = (o1, o2) -> o1.getTime().
compareTo(o2.getTime());
List<Order> tempOrders = this.orders.stream().
filter(o->o.getStatus()==status).
collect(Collectors.toList());
Collections.sort(tempOrders,
byRestaurantName
.thenComparing(byCustomer1name)
.thenComparing(byCustomer2name)
.thenComparing(byTime));
String res = "";
// for each order in the sorted orders list
for (Order order : tempOrders){
res+=order.toString();
}
return res;
}