Multiple differing views of the same list

48 views Asked by At

I'm trying to make iterators for a class called Ship. This is for a genetic algorithm example.

The two key attributes of Ship are weight and volume (knapsack problem). I want one iterator sorting by weight, and one by volume.

I am having trouble setting up the implements Comparable<(?)> interface for the Ship class.

At first I tried to pass a Class called Value which has an enum representing its type and a float value. I have a CompareTo method that looks like this:

return int CompareTo(Value arg0)

 switch (arg0.type())

 case WEIGHT:
  return this._weight.compareTo(arg0.value);

...
1

There are 1 answers

1
Praba On BEST ANSWER

You'd be better of having two comparators one comparing based on weight and one based on volume and the use those comparators to sort on demand.