I'm working on a project where I have a table in memory, for example:
field1 field2 field3 field4 field5
somevalue somevalue somevalue somevalue somevalue
somevalue somevalue somevalue somevalue somevalue
.
.
.
.
v
The key thing to realize here is that your search algorithm always stays exactly the same. The only thing that changes is your actual comparison function.
I would recommend using a standard sort in the Arrays
or Collections
class, but writing your own custom Comparable
class or Comparator
, whose compare
method will use the data the user supplies. This is actually kind of interesting - I've only ever written Comparable
classes that use fixed data, but this is exactly a use case for them.
All you need to do is write the function
public int compare(Row row1, Row row2) {
// iterate over the columns *in order of the user-supplied priority*
// return appropriately
}
Here's documentation on the Comparable class.
Which one you need depends on what you're implementing, I forget the details but should be straightforward.
The key #2 thing here is that you can instantiate your ColumnPrioritiesComparable
class before the priorities are set. e.g.
class ColPriComp implements Comparable<ColPriComp> {
private volatile int[] priorities; // possibly static, or obtained some other way,
// so there's only one shared among all classes
@Override
public int compareTo(ColPriComp other) {
// use "priorities" to do the comparison
}
public void setPriorities(int[] newPriorities) {
this.priorities = newPriorities;
}
}