-2

I'm creating a program where I have to make a database for an university. I used a map to save data and I need to sort it by attribute course to show the name and the code of the subject, but I don't know how to do it. Thanks a lot!

 public class Student {
    private String apellido = "";
    private String nombre = "";
    private String correo = "";
    private String dni = "";

    public Student(String lastname, String name, String mail, String id)                
    {
        this.lastname = lastname;
        this.name = name;
        this.mail = mail;
        this.id = id;
    }
}


public class Subject {
    private String name = "";
    private int code
    private int course;

    public Subject(String name, int code, int course) {
        this.name = name;
        this.code = code;
        this.course = course;
    }

    public String getName() {
        return name;
    }

    public int getCode() {
        return code;
    }

    public int getCourse() {
        return course;
    }
}


public class Model {
private Map<Subject, List<Student>> map = new HashMap<>();

    public void orderByCourse() {
    System.out.printf("%-20s %-6s\n", "subject", "code");
    List<String> list = new ArrayList<String>();
    for (Entry<Subject, List<Student>> element : map.entrySet()){
        Subject key = element.getKey();
        List<Student> value = element.getValue();
        System.out.printf("%-20s %-6s\n", key.getName(), key.getCode());
    }
}
Daphne
  • 53
  • 1
  • 6
  • Does the `value` of the entry matter? Or are you only concerned with the `key`? – 4castle Dec 18 '16 at 23:53
  • @4castle I know, it is somehow a duplicate of this question. – msagala25 Dec 18 '16 at 23:53
  • @msagala A question is a duplicate to itself? How meta ... – Tom Dec 18 '16 at 23:54
  • @Tom my *it* is pertaining to the link that I gave earlier, and my *this* is this question. read. – msagala25 Dec 18 '16 at 23:57
  • @msagala And you don't get it into your head that your "it" is "this"? That you've linked ***this*** question in the comment? – Tom Dec 19 '16 at 00:02
  • @Tom nope, *this* is the current question. not the link that I gave. just read. – msagala25 Dec 19 '16 at 00:06
  • @msagala I think you need to read the question you linked to. Look at the url for this question, and look at the url for that question. It's the same question. Now let's stop spamming the comments. – 4castle Dec 19 '16 at 00:08
  • 1
    @msagala Your incapability in re-checking the question you've linked is amazing. You don't even need to read the full URL, just check the question id, 41214085. – Tom Dec 19 '16 at 00:14

1 Answers1

0

Technically speaking, you cannot sort a Map. The order / ordering of the keys and values in a Map are controlled by the Map implementation.

What you can do is take a copy of a map's key-set, value-set or entry-set and then sort that copy. However, it is important to note that this does NOT affect the order of the original map's entries.

So your Question really boils down to two sub-Questions:

Question: How do you copy the respective set?

Answer: Use the ArrayList(Collection) copy constructor.

Question: How do you sort an array list based on an attribute of the list's element class?

Answer: Create a Comparator object for the element class that orders a pair of element instances based on the values of the required attribute. See also Sort ArrayList of custom Objects by property.

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216