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
- 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.
- 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.