# Ordenar una List Java 8

Ordenar List con Java 8

{% embed url="<http://avbravo.blogspot.com/2014/12/ordenar-list-y-agrupar-con-java-8.html>" %}

Agrupar

```java
 Map<String, List<Consolidado>> listAgrupado = listConsolidado
                    .stream()
                    .collect(Collectors.groupingBy(p -> p.getIdnivel6()));
```

Ordenar

```java
  Collections.sort(listConsolidadoFinal,
                    (Consolidado a, Consolidado b) -> a.getIdnivel5().compareTo(b.getIdnivel6()));
```

Ordenar un List<> en Orden inverso

```java
List<Solicitud> solicitudList = solicitudRepository.findAll();

solicitudList.sort(Comparator.comparing(Solicitud::getIdsolicitud)
                      .reversed()
                      .thenComparing(Comparator.comparing(Solicitud::getIdsolicitud)
                      .reversed())
```

<http://avbravo.blogspot.com/2014/12/ordenar-list-y-agrupar-con-java-8.html>

## Jugando con Lambda JDK8

{% embed url="<http://avbravo.blogspot.com/2014/08/jugando-con-lambda-jdk8.html>" %}

Otras comparaciones

{% embed url="<https://dzone.com/articles/java-8-comparator-how-to-sort-a-list>" %}

Ejemplo del enlace anterior

```java
List<Movie> movies = Arrays.asList(
        new Movie("Lord of the rings", 8.8),
        new Movie("Back to the future", 8.5),
        new Movie("Carlito's way", 7.9),
        new Movie("Pulp fiction", 8.9));
movies.sort(Comparator.comparingDouble(Movie::getRating)
                      .reversed());
movies.forEach(System.out::println);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/trucosjakartaee/overview/java-8/list/ordenar-una-list-java-8.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
