1

Problem

I have a list of Student objects (I just wrote the list in this way so that it becomes more readable):

class Student {
    int id;
    String name;
    String department;
}

[
    {"id": 1, "name": "some name 1", "department": "English"},
    {"id": 2, "name": "some name 2", "department": "Maths"},
    {"id": 3, "name": "some name 3", "department": "Science"}
]

I want to sort this list on the basis of the custom order of the department.

["Maths", "Science", "English"]

The final sorted list should look like:

[
    {"id": 2, "name": "some name 2", "department": "Maths"},
    {"id": 3, "name": "some name 3", "department": "Science"},
    {"id": 1, "name": "some name 1", "department": "English"}
]

What I tried

  1. I iterated the list and made list of all the departments and sorted all the list and in the end, I clubbed all the list. but it was taking to much extra space is not optimize enough.
  2. I tried using comparable but with that, I was only able to sort an object by a field.

What I want

I want to sort by id, but before that.. I want to sort the departments as well. The sorting of department will be based on the list of department. The first should be Maths, second should be Science and the last should be English.

I want some suggestions, like how can I achieve this with a better performance.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
yashjain12yj
  • 723
  • 7
  • 19
  • What is the custom sort order? – Unknown Dec 09 '19 at 11:48
  • The option 2 you tried out, isn't that what you wanna do? i.e. sort by a field, just that it is a custom order. What's the problem? You can just add in the logic what order you want, and return value based on that. (I would rather suggest a comparator for better design though, instead of comparable, in this case) – A_C Dec 09 '19 at 11:49
  • Isn't this what you want? https://stackoverflow.com/a/3704930/5027073 – Unknown Dec 09 '19 at 11:58
  • use e.g. Collections.sort with a Comparator where you decode the department's rank by getting it from e.g. a HashMap, that maps their name to their rank: {Math->1}{Science->2}{English->3} – Curiosa Globunznik Dec 09 '19 at 11:59
  • 1
    No, that question is different from this one. @daniu – yashjain12yj Dec 09 '19 at 13:08
  • 1
    How to give a rank from Comparator @gurioso – yashjain12yj Dec 09 '19 at 13:10
  • as mentioned above. [comparator.comparing](https://www.baeldung.com/java-8-comparator-comparing) – Curiosa Globunznik Dec 09 '19 at 13:11
  • 1
    Yes, I read your comment but how should I achieve this. How I should implement the comparator using this hashmap. – yashjain12yj Dec 09 '19 at 13:13

1 Answers1

1

You could create an enum holding the different departments with their priority. i.e.

enum SchoolDepartmentEnum{
ENGLISH(1,3,"English"),

MATHS(2,1,"Maths"),

SCIENCE(3,2,"Science");

constructor:  SchoolDepartmentEnum(int id, int priority, string name);

}

Then create a Comparator class which will use the priority ID from the enum you created.

  • 1
    How I should I use this enum in comparator? – yashjain12yj Dec 09 '19 at 13:11
  • You can call the Comparator (btw you can create your own comparator implementing Java's Comparator) when you need it, and sort your list with a stream like "list.stream().sorted(new YourComparatorClass())". – George Zenios Dec 09 '19 at 13:23