only just started learning java and was stuck on this problem.
Let's say I have a list of employees (I'll use only three of this examples) in no particular order and I go through the list and create a sorted link of nodes that all contain a name and salary per week. These three nodes would individually look like so:
(John, 1000)
(Bob, 1000)
(Adam, 1000)
And I wanted to sort it first by salary then alphabetically by name so all the nodes connected would look something like this:
(Adam, 1000)(Bob, 1000)(John, 1000)
I also have a way of increase the salary so if I were to do something like bobNode.increaseBy(200) (the amount increased at a time will always be the same i.e 200 every time the method is called for every name) the connected nodes would update and look something like this
(Bob, 1200)(Adam, 1000)(John, 1000)
Is there any efficient or easy way to do this? Currently, I have a compareTo method in my Node class that returns this.name.compareTo(other.name) so the nodes are sorted alphabetically as I go through the list of employees. Is there anyway to check for both conditions and sort?
I was thinking about doing something like if salary.compareTo(other.salary) == 0 compare the names instead, but since the nodes would already exists in the linked list it wouldn't really work.
What about adding and sorting the nodes alphabetically first and whenever salary of a node is adjusted removing that node and adding it again to the correct position?
Another idea I had that's similar to the previous was to remove the adjusted nodes and creating a new linked list of nodes that contains all the nodes with that amount of salary sorted alphabetically. I will then add these new nodes after I have gone through the list of employees. Wouldn't this be a bit problematic if say I had a list of 200,000 employees with a wide range of salary thus I would have to create and iterate through many nodes?
I also wanted to note that the salary can increase without having all the employees be added first.
Any help and ideas would be greatly appreciated!