Java to sort list of custom object on the basis of string

前端 未结 7 1086
灰色年华
灰色年华 2021-01-22 18:02
class Person 
 {
 private String name;
 private String profession;
}

profession has values:

  • engineer
  • Doctor<
7条回答
  •  终归单人心
    2021-01-22 18:41

    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

提交回复
热议问题