BBjStandardGrid::sortByColumn

Description

In BBj 6.0 and higher, this BBjStandardGrid method sorts a grid by the content of a given column.

Syntax

Return Value

Method

void

sortByColumn(int columnIndex, int sortOrder)

Parameters

Variable

Description

columnIndex

Specifies the zero-based index on which to sort.

sortOrder

Specifies one of the values listed below:

SORT_ASCENDING

SORT_DESCENDING

SORT_NONE 

Return Value

None.

Remarks

If the cells of the column do not all have the same style (e.g. if there is a combination of InputN and InputD styles in the column), the sortByColumn(columnIndex, sortOrder) will fail.

After sorting any column of a grid, be aware that the grid contains two differing coordinate systems: view coordinate and model coordinate. A view coordinate is the (row, column) at which a given cell displays in the grid, and the model coordinate is the (row, column) which the program uses to get/set cell attributes. All methods of the grid use model coordinates.

 

For example, if grid! is a grid containing three rows and one column, the following calls will place "B" in the last row of the grid and a getCellText(2,0) returns a "B":

   Grid!.setCellText(0,0,"A")

   Grid!.setCellText(1,0,"C")

   Grid!.setCellText(2,0,"B")

After the program calls Grid!.sortByColumn(0,1), "C" appears in the last row of the grid. The model coordinates of "C" are still (1,0) and the view coordinates of "C" are (2,0). The grid's methods always use model coordinates and calling Grid!.getCellText(1,0) will return "C." As a result, the program refers to the cell using the model coordinates of (1,0) while the user sees the cell at the view coordinates of (2,0).

When sorting a grid by multiple columns, the sort occurs in the same order the columns were originally sorted.

Example

rem 'sort the grid by column

rem 'Obtain the instance of the BBjAPI object
let myAPI!=BBjAPI()

rem 'Open the SysGui device
SYSGUI=UNT
OPEN (SYSGUI) "X0"

rem 'Obtain the instance of the BBjSysGui object
let mySysGui!=myAPI!.getSysGui()

rem 'Set addWindow param values
X=10
Y=10
WIDTH=300
HEIGHT=300
TITLE$="BBj Window - Sort"

rem 'Set the current context
mySysGui!.setContext(0)

rem 'Create a window
myWindow! = mySysGui!.addWindow(X,Y,WIDTH,HEIGHT,TITLE$,$000002$)

rem 'Add a grid on the window
myGrid! = myWindow!.addGrid(101,25,25,250,200,$8060$,10,2)

rem 'Add button to the window
sortButton!=myWindow!.addButton(102,90,230,120,30,"unsort the Grid",$$)

rem 'Set the grid properties
myGrid!.setDefaultColumnWidth(130)
myGrid!.setGridEditable(1)
myGrid!.setFitToGrid(1)
myGrid!.setRowHeight(16)
myGrid!.setEditable(1)
myGrid!.setHasColumnHeader(1)

rem 'Set the text for the column header
myGrid!.setColumnHeaderCellText(0,"Sort")

rem 'set the first column sortable by clicking on the Header
myGrid!.setColumnUserSortable(0,1)

rem 'Add text to the grid cells
FOR ROW = 0 TO 9
    FOR COL = 0 TO 1
        CELLTEXT$=STR(ROW+1) +" " + STR(COL+1)
        myGrid!.setCellText(ROW,COL,CELLTEXT$)
    NEXT COL
NEXT ROW

rem 'Sort the grid-descending by first column initially
myGrid!.sortByColumn(0,-1)

rem 'Register the CALLBACK routines
myGrid!.setCallback(myGrid!.ON_GRID_EDIT_STOP,"CALL_RESORT")
myWindow!.setCallback(myWindow!.ON_CLOSE,"APP_CLOSE")
sortButton!.setCallback(sortButton!.ON_BUTTON_PUSH,"SORT_OPTIONS")

rem 'Process Events
process_events

rem 'Callback routine called when the user closes the application window
APP_CLOSE:
release

CALL_RESORT:
    PRINT "calling resort"
    myGrid!.resort()
return

SORT_OPTIONS:
    if (sortButton!.getText() = "unsort the Grid") then
        myGrid!.unsort()
        myGrid!.setColumnUserSortable(0,0)
        myGrid!.setColumnHeaderCellText(0,"")
    else
        myGrid!.setColumnUserSortable(0,1)
        myGrid!.setColumnHeaderCellText(0,"Sort")
    endif

    rem 'determine if the grid is sortable or not and set button text
    if (myGrid!.isColumnUserSortable(0) = 0) then
        sortButton!.setText("Sort the Grid")
    else
        sortButton!.setText("unsort the Grid")
    endif
return

See Also

BBjAPI

BBjSysGui

BBjControl

BBjWindow

See the BBj Object Diagram for an illustration of the relationship between BBj Objects.