Reputation: 444
I have a table with a bunch of columns, some visible, but the most invisible. The invisible Columns are all sorted alphabetically, using a comparator that ignores the visible columns and it all works fine (yay!).
But I would like to have the visible columns in a specific order. Say I have the visible columns 'pain', 'gain', 'sprain', 'rain' and 'lame' which are by default not ordered or sorted in any defined way and I want them in the specific order 'gain', 'rain', 'sprain', 'lame' and 'pain'
How can I accomplish that?
Upvotes: 0
Views: 528
Reputation: 209340
You can put the order into a map:
Map<String, Integer> columnOrder = new HashMap<>();
String[] order = new String[] {"gain", "rain", "sprain", "lame", "pain"};
for (int i = 0 ; i < order.length ; i++) {
columnOrder.put(order[i], i);
}
And then just use the Comparator
API to provide a comparator for the ordering. It seems to me it really doesn't matter what the order of the invisible columns is, since you can't see them, but you can do something like
Comparator<TableColumn<?, ?>> columnComparator =
Comparator.comparing(TableColumn<?,?>::isVisible).
.thenComparing(col -> columnOrder.getOrDefault(col.getText().toLowerCase(), Integer.MAX_VALUE));
Defaulting to MAX_VALUE
just ensures that any column with text not in the map goes at the end.
Upvotes: 1