class Person
{
private String name;
private String profession;
}
profession has values:
- engineer
- Doctor<
My suggestion -
1. Create a new class Profession
-
class Profession{
public Profession(Integer id, String prefessionName){
this.id=id;
this.prefessionName=prefessionName;
}
Integer id;
String professionName;
}
2. Now give Id to each Profession
object/instance maintaining the order. For example -
Profession engineer = new Profession(1, "Engineer"); //since Engineer is in first place
Profession doctor = new Profession(2, "Doctor"); //since Doctor is in second place
Profession teacher = new Profession(3, "Teacher");
Profession student = new Profession(4, "Student");
3. Now sort the Profession
for id
using compareable
interface.
You can sort your custom object using Collection.sort
method like this,
Collections.sort(list, new Comparator(){
public int compare(Object o1, Object o2) {
Person p1 = (Person) o1;
Person p2 = (Person) o2;
return p1.getProfession().compareToIgnoreCase(p2.getProfession());
}
});
To Sort
in reverse order
just make your return
statement line like this,
p2.getProfession().compareToIgnoreCase(p1.getProfession());
This will directly make your list
sorted.
Two ways:
Comparable
interfaceComparator
With the first way you can sort the collection only by the one method compareTo
you will define
Your Person.java
class Person implements Comparable<Person> {
private String name;
private String profession;
@Override
public int compareTo(Person o) {
return this.profession.compareTo(o.getProfession());
}
public String getName() {
return name;
}
public String getProfession() {
return profession;
}
}
Then you need to call:
Collections.sort(yourCollection);
With the second way you can sort by one or more Comparator
, giving you the ability to compare the same collection with different criteria.
Example of two Comparator
public class PersonSortByNameComparator implements Comparator<Person>{
@Override
public int compare(Person p1, Person p2) {
return p1.getName().compareTo(p2.getName());
}
}
public class PersonSortByAlphabeticalProfessionComparator implements Comparator<Person>{
@Override
public int compare(Person p1, Person p2) {
return p1.getProfession().compareTo(p2.getProfession());
}
}
Or this one you need:
public class PersonSortByProfessionComparator implements Comparator<Person> {
@Override
public int compare(Person p1, Person p2) {
if(p1.getProfession().equalsIgnoreCase(p2.getProfession())
return 0;
if(p1.getProfession().equalsIgnoreCase("student")
return -1;
if(p1.getProfession().equalsIgnoreCase("engineer")
return 1;
if(p1.getProfession().equalsIgnoreCase("doctor")
return 1;
else
return -1;
}
}
And then call one of them:
Collections.sort(yourCollection, new PersonSortByNameComparator());
This blog article is really good written and you can some examples
Add enum to your variables:
class Person {
private String name;
private String profession;
private enum enumProffesion
{
Doctor, Teacher, student;
}
}
After that could add function to the Person class which would give you value of the profession:
public int professionValue()
{
enumProffesion enumValue= enumProffesion.valueOf(proffesion);
switch (enumValue) {
case Doctor: return 1; break;
case Teacher: return 2; break;
case student: return 3; break;
default: return null;
}
}
After that you just implement logic that will sort all Persons. For that you can help your self with this answer: sorting integers in order lowest to highest java
Comparable
Yes you can implement Comparable. Create a method compareTo(Person obj) and then write your custom logic. You can compare alphabetically or whatever other algorithm you want - for example engineer before doctor because he makes more money :) For alphabetic comparing you can do it like that:
class Person implements Comparable<Person> {
@Override
public int compareTo(Person o) {
return this.profession.compareTo(o.getProfession());
}
private String name;
private String profession;
}
After that you just use the Collections.sort
You can do it easily if replace profession
field by enum
:
class Person implements Comparable<Person> {
private String name;
private Profession profession;
// by this we'll define natural ordering based on ordering
// in which `Profession` enum values are declared
@Override
public int compareTo(Person p) {
return this.profession.compareTo(p.profession);
}
}
And here's Profession
enum:
public enum Profession {
ENGINEER("engineer"), DOCTOR("Doctor"), TEACHER("Teacher"), STUDENT("student");
private String displayName;
Profession(String dn) {
this.displayName = dn;
}
@Override
public String toString() {
return this.displayName;
}
}
If you are new to the enum facility in Java, see Oracle tutorial.