Level.java Source code

Java tutorial

Introduction

Here is the source code for Level.java

Source

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

enum Level {
    HIGH, MEDIUM, LOW;
}

class Person {
    private Level severity;
    private String name;

    public Person(Level severity, String name) {
        super();
        this.severity = severity;
        this.name = name;
    }

    public Level getSeverity() {
        return severity;
    }

    public void setSeverity(Level severity) {
        this.severity = severity;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Main {
    public static void main(String[] args) {
        Person one = new Person(Level.HIGH, "A");
        Person two = new Person(Level.MEDIUM, "B");
        Person three = new Person(Level.LOW, "C");
        Person four = new Person(Level.HIGH, "D");
        Person five = new Person(Level.MEDIUM, "E");
        Person six = new Person(Level.LOW, "F");
        Person seven = new Person(Level.LOW, "G");

        List<Person> persons = new ArrayList<Person>();
        persons.add(one);
        persons.add(two);
        persons.add(three);
        persons.add(four);
        persons.add(five);
        persons.add(six);
        persons.add(seven);

        Collections.sort(persons, new Comparator<Person>() {
            @Override
            public int compare(Person person1, Person person2) {
                if (person1.getSeverity() == person2.getSeverity()) {
                    return person1.getName().compareTo(person2.getName());
                } else {
                    return person1.getSeverity().compareTo(person2.getSeverity());
                }
            }
        });
        for (Person person : persons) {
            System.out.println(person.getName() + " " + person.getSeverity());
        }
    }
}