Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try below code </p> <p>Create an <code>ENUM</code></p> <pre><code>package com.rais; public enum Severity { HIGH("H"), MEDIUM("M"), LOW("L"); private final String value; private Severity(String value) { this.value = value; } } </code></pre> <p>Now Create <code>Person</code> class according to your requirement eg.</p> <pre><code>package com.rais; public class Person { private Severity severity; private String name; public Person(Severity severity, String name) { super(); this.severity = severity; this.name = name; } public Severity getSeverity() { return severity; } public void setSeverity(Severity severity) { this.severity = severity; } public String getName() { return name; } public void setName(String name) { this.name = name; } } </code></pre> <p>Finally create a Test Client and apply below logic. </p> <pre><code>package com.rais; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class TestClient { public static void main(String[] args) { Person one = new Person(Severity.HIGH, "shayam"); Person two = new Person(Severity.MEDIUM, "mohan"); Person three = new Person(Severity.LOW, "radha"); Person four = new Person(Severity.HIGH, "rakesh"); Person five = new Person(Severity.MEDIUM, "kailash"); Person six = new Person(Severity.LOW, "rais"); Person seven = new Person(Severity.LOW, "abhishek"); List&lt;Person&gt; persons = new ArrayList&lt;Person&gt;(); 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&lt;Person&gt;() { @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()); } } } </code></pre> <p>I am sure you will get below output. </p> <pre><code>rakesh HIGH shayam HIGH kailash MEDIUM mohan MEDIUM abhishek LOW radha LOW rais LOW </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload