RFR: 8301761: The sorting of the SortedList can become invalid [v2]
Loay Ghreeb
duke at openjdk.org
Sat Aug 10 07:12:37 UTC 2024
On Fri, 9 Aug 2024 19:11:24 GMT, John Hendrikx <jhendrikx at openjdk.org> wrote:
> This is not the case. The sorting is still correct. Claiming that the sorting is invalid because a newly added item was not placed in a specific location relative to other equal items is a bit of misrepresentation.
>
> So, if this PR wants to move forward, I think we first need to decide if we want to guarantee a specific behavior when inserting new but equal elements.
The issue isn’t just with adding new elements. It also affects the `TableView` that populated with items from a `SortedList`, The `SortedList`'s comparator is bound to the `TableView`'s comparator.
When the table is sorted by multiple columns, removing a column from the sort order should reorder the items. However, currently, removing any column from the sort order doesn't change the items order until all sorting columns are removed. At that point, the items returned to the default insertion order.
For example:
![image](https://github.com/user-attachments/assets/9ed8b2ea-9879-4dc9-a285-4d03be2d2691)
1. The initial order is [0, 1, 2, 3] (index column).
2. Sort by `Col1`, the order will be: [1, 0, 2, 3].
3. Add a second sort (Shift + Click) on `Col2`, the order will be: [1, 2, 3, 0].
4. Shift + Click again on `Col2` to sort in descending order: [1, 0, 3, 2].
5. Shift + Click again on `Col2` to remove the sort from `Col2`. The order will stay the same [1, 0, 3, 2], but the expected order is [1, 0, 2, 3] as in step 2.
<details>
<summary> Code </summary>
import javafx.application.Application;
import javafx.beans.property.ReadOnlyObjectWrapper;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setScene(new Scene(createTableView()));
primaryStage.show();
}
private TableView<Data> createTableView() {
TableView<Data> tableView = new TableView<>();
TableColumn<Data, Integer> indexColumn = new TableColumn<>("#");
indexColumn.setCellValueFactory(cell -> new ReadOnlyObjectWrapper<>(cell.getValue().index()));
indexColumn.setSortable(false);
TableColumn<Data, Integer> column1 = new TableColumn<>("Col1");
column1.setCellValueFactory(cell -> new ReadOnlyObjectWrapper<>(cell.getValue().val1()));
TableColumn<Data, Integer> column2 = new TableColumn<>("Col2");
column2.setCellValueFactory(cell -> new ReadOnlyObjectWrapper<>(cell.getValue().val2()));
tableView.getColumns().addAll(indexColumn, column1, column2);
ObservableList<Data> data = FXCollections.observableArrayList(
new Data(0, 5, 4),
new Data(1, 1, 7),
new Data(2, 5, 2),
new Data(3, 5, 3)
);
SortedList<Data> sortedData = new SortedList<>(data);
sortedData.comparatorProperty().bind(tableView.comparatorProperty());
tableView.setItems(sortedData);
return tableView;
}
public record Data(int index, int val1, int val2) {
}
}
</details>
-------------
PR Comment: https://git.openjdk.org/jfx/pull/1519#issuecomment-2279897597
More information about the openjfx-dev
mailing list