Planet JFX

Bubble Sort of Tree Cells

118pages on
this wiki
Add New Page
Add New Page Talk0

In working with Trees in JavaFX, you might have a need to alpha sort child TreeCells in the event that a label is edited or a new tree cell is inserted. This example uses a simple Bubble Sort implementation to trigger the sort of TreeCells that are the child of the indicated parent.

For this example, I am using a subclass of TreeCell that adopts a new operation called sortCells() which can be called upon it at any time.

public class MyTreeCell extends TreeCell {
    public operation sortCells();  // trigger sorting of child cells

operation MyTreeCell.sortCells() {
	if (sizeof this.cells > 1) {
	  // bubble sort
 	  for (i in [0..(sizeof this.cells)-2]) {
    	    for (j in [(sizeof this.cells)-1..i+1]) {
				var cell1 = this.cells[j-1];
				var cell2 = this.cells[j];
				if (cell1.text.compareToIgnoreCase(cell2.text) > 0) {
					//swap( A[ j ],  A[ j - 1 ] )
					insert cell2 before this.cells[j-1];
					delete this.cells[j+1];

--Rcasey 00:18, 17 November 2007 (UTC)

Also on Fandom

Random Wiki