> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud6-solucions/interfaces-comparable-y-comparator.md).

# Interfaces: Comparable y Comparator

```java
package Comparable.Ejercicio1;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Paciente implements Comparable<Paciente> {
    private String dni;
    private String nome;
    private LocalDateTime dataIngreso;
    private int prioridade; // 1: Leve, 2: Grave, 3: Moi Grave

    public Paciente(String dni, String nome, LocalDateTime data, int prioridade) {
        this.dni = dni;
        this.nome = nome;
        this.dataIngreso = data;
        this.prioridade = prioridade;
    }

    // Getters e Setters
    public String getDni() { return dni; }
    public String getNome() { return nome; }
    public LocalDateTime getDataIngreso() { return dataIngreso; }
    public int getPrioridade() { return prioridade; }

    @Override
    public int compareTo(Paciente outro) {
        return this.dataIngreso.compareTo(outro.dataIngreso);
    }

    @Override
    public String toString() {
        return String.format("DNI: %s | Nome: %-8s | Ingreso: %s | Prioridade: %d",
                dni, nome, dataIngreso, prioridade);
    }
}
```

```java
package Comparable.Ejercicio1;

import java.util.Comparator;

public class ComparadorGravidade implements Comparator<Paciente> {

    @Override
    public int compare(Paciente o1, Paciente o2) {
        if (o1.getPrioridade()!=o2.getPrioridade())
            //Si el menor es el que va primero o1.getPrioridade()-o2.getPrioridade();
            //Si el mayor es el que va primero o2.getPrioridade()-o1.getPrioridade();
            return o2.getPrioridade()-o1.getPrioridade();
        //Desempatamos empleando el orden natural
        return o1.compareTo(o2);
    }
}
```

```java
package Comparable.Ejercicio1;

import java.util.Comparator;

public class ComparadorDNI implements Comparator<Paciente> {
    @Override
    public int compare(Paciente p1, Paciente p2) {
        return p1.getDni().compareTo(p2.getDni());
    }
}

```

```java
package Comparable.Ejercicio1;

import java.util.*;

public class CentroSaude {

        private Deque<Paciente> colaPacientes;

        public CentroSaude() {
            this.colaPacientes = new ArrayDeque<>();
        }

        public void engadirPaciente(Paciente p) {
            colaPacientes.addLast(p);
        }

        public void atenderPaciente() {
            if (!colaPacientes.isEmpty()) {
                Paciente atendido = colaPacientes.removeFirst();
                System.out.println("Atendendo a: " + atendido.getNome());
            } else {
                System.out.println("Non hai pacientes en espera.");
            }
        }

        public void mostrarPorOrdenChegada() {
            List<Paciente> lista = new ArrayList<>(colaPacientes);
            Collections.sort(lista);
            System.out.println(lista);
        }

        public void mostrarPorGravidade() {
            List<Paciente> lista = new ArrayList<>(colaPacientes);
            Collections.sort(lista, new ComparadorGravidade());
            System.out.println(lista);
        }

        public void mostrarPorDNI() {
            List<Paciente> lista = new ArrayList<>(colaPacientes);
            Collections.sort(lista, new ComparadorDNI());
            System.out.println(lista);
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("--- Lista actual no centro ---\n");
            for (Paciente p : colaPacientes) {
                sb.append(p).append("\n");
            }
            return sb.toString();
        }
}

```

```java
package Comparable.Ejercicio1;

import java.time.LocalDateTime;

public class App {
    public static void main(String[] args) {
        CentroSaude centro = new CentroSaude();

        LocalDateTime ld1 = LocalDateTime.of(2025,4,2,17,00,00);
        LocalDateTime ld2 = LocalDateTime.of(2025,4,1,11,00,00);
        LocalDateTime ld3 = LocalDateTime.of(2025,4,1,9,00,00);
        LocalDateTime ld4 = LocalDateTime.of(2025,4,3,10,00,00);
        // Engadir pacientes
        centro.engadirPaciente(new Paciente("78932145J", "Santiago", ld1, 3));
        centro.engadirPaciente(new Paciente("12356789A", "Ana", ld2, 2));
        centro.engadirPaciente(new Paciente("43576789K", "María", ld3, 2));
        centro.engadirPaciente(new Paciente("65743219Z", "Pedro", ld4, 1));

        System.out.println("\n>>> PACIENTES POR ORDE DE CHEGADA:");
        centro.mostrarPorOrdenChegada();

        System.out.println("\n>>> PACIENTES POR GRAVIDADE:");
        centro.mostrarPorGravidade();

        System.out.println("\n>>> PACIENTES POR DNI:");
        centro.mostrarPorDNI();
    }
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://educacion.gitbook.io/programacion/exercicios-java/ud6-solucions/interfaces-comparable-y-comparator.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.
