0

I currently am writing a Java program that matches users to recommended student groups based on their answers to questions regarding special interests, major, etc. Each user is defined by a User object. Each student group is defined by a Group object.

I have a working method of assigning each specific Group instance a particular score- for example, the Group schoolNewspaper has a score of 72, and the group filmMakers has a score of 99.

Each Group object also has an integer value, called

int difference;

This represents the similarities by absolute value between the user's score and the score of the clubs. For example, if someone takes the quiz and gets 71, the difference value for the schoolNewspaper is assigned to 1, and the filmMakers Group is assigned a difference value of 26.

The user's score is compared to all of the Group scores, thereby assigning each Group a difference value. I would like to form a linked list containing all of the Groups, then order it such that those with the smallest difference value are at the head of the list. This is because I will be using the 5 most similar Groups to create a recommendation for the user, and I feel it would be more effective to sort the list and take the first 5 elements rather than searching repeatedly.

I am not sure if a current sort method exists in the Java libraries that might make this process easier- as sometimes I try to reinvent the wheel without realizing how certain library operations can be used. So, is there a particular way of using an integer value defined within a unique object to order a linked list of such objects? I'm relatively new to programming, so any help in terms of approach/theory would be greatly appreciated.

Edit: Perhaps another data structure may be better for this sorting rather than a linked list?

rubyquartz
  • 319
  • 1
  • 2
  • 11
  • Thanks! I actually read this question first, but it involved sorting by strings using a library operation. This deals specifically with integers – rubyquartz Jun 17 '17 at 17:14
  • And why can't you use the same answer for your sorting of integers? – Joe C Jun 17 '17 at 17:16

1 Answers1

2

You could use the Comparable interface to sort the list of Groups using Collections.sort(List<Group> groups):

Example of Group implementation:

public class Group implements Comparable{
    private int difference;
    ... //code

    public int compareTo(Group g) {
        int diff = g.getDiff();
        if(diff > this.difference) { // compared Group obj has greater diff
            return -1;
        }else if(diff == this.difference) { // compared Group obj has equal diff
            return 0;
        }else { // compared Group obj has lesser difference
            return 1;
        }
    }
}

Just as a side note:

By saying: "Each Group object also has an integer value, called

int difference;

This represents the similarities by absolute value between the user's score and the score of the clubs. For example, if someone takes the quiz and gets 71, the difference value for the schoolNewspaper is assigned to 1, and the filmMakers Group is assigned a difference value of 26."

Are you saying that the program gives a user immediate feedback when he/she is using the program, or is it collectively based?

If it is the latter, which I assume by the User-object, then collecting the different scores of the users would be more efficient. Then you just calculate the difference in a method, passing in each User-object with their different scores and use a sorting implementation, similar to the one mentioned above, on each user.

The User object can then be passed a top-5 list of the matched Group-objects. This way, you won't have to update the difference value of every Group-object nor will you need to create multiple instances of each group. :)