Documents
NextGrid & NextDBGrid
- Html Column Tutorial
- Upgrade existing projects for loading changes
- Edit events in NextGrid
- NextGrid Vista Style
- NextGrid How To
- Integrating Inplace Editors
- NextGrid Slide Style Tutorial
- InputLine Tutorial
- FieldChooser Tutorial
- Using Editors in runtime
- Grid Report Tutorial
NextGrid
- TreeColumn Tutorial
- NextGrid Quick Start
- Custom Draw in NextGrid
- Export to XML from NextGrid
- Optimize NextGrid
- NextGrid Custom Sorting
- VirtualColumn Tutorial
- Graphic Column Tutorial
NextDBGrid
- NextDBGrid Quick Start
- NextDBGrid Events
- NextDBGrid How To
- Sorting records in NextDBGrid
- LookupColumn Tutorial
NextInspector
- NextInspector Advanced tutorial
- ToolbarItem tutorial
- NextDBInspector Tutorial
- NextInspector Item Types
- Map VCL property to item
- NextInspector Quick Start
NextSheet
NextCollection
- NxAlertWindow Tutorial
- NxInfoPanel Tutorial
- Vista Styled Panels
- NxPathControl and NxNotebook tutorial
- NxPathControl Quick Start
- NxFlipPanel and NxHeaderPanel Quick Start
- NxOutlookBar Tutorial
- NxPageControl and NxNotebook Quick Start
- NxButton Tutorial
Misc
- Component Names Change
- NxComboBox styles
- Numeric FormatMask
- NxVirtualDataSet tutorial
- DateTime FormatMask
- NxPreview Quick Start
- 32bit Bitmaps Tutorial
- Quick update
- Enable typing unicode characters in InplaceEditors
- Using NxColorScheme
- NxProgress Tutorial
- NxAutoCompletion Tutorial
Labs
NextGrid .NET
NextGrid Custom Sorting
Related Articles:
Info
- Skill:
- Version: 1.0.0
- Author: Bojan Nikolic
- Created:
- Updated: 2008-04-21
Downloads
Introducing
With using Custom Sorting technique inside NextGrid, cells may be sorted inside Column by any criteria.
For example, sort Column with next criterias: Months by Names, Planets Names by sorting their Sizes, Rome Numbers, Color by Names, and any other criteria.
Turning On Custom Sorting
First, custom sorting need to be enabled by set SortType property of Column to stCustom
Defining sorting criteria logic
With using our own custom sorting criteria only we can know how Column will be sorted, and we need to help Grid to determine which data will be higher in sorted list and which will be lower. This need to be done inside OnCompare event.
This event have 3 very important parameters. We need to compare Cell1 and Cell2 parameters with using our custom sorting logic, and then set Compare parameter to one of theese values: 1, 0, -1
Value 1 mean that by our sorting logic Cell1 is "larger" than Cell2. Value 0 determine that Cells are equal and -1 determine that Cell2 is "larger" that Cell1.
How to do this?
Here is one very simple example:
procedure TForm1.NextGrid1Compare(Sender: TObject; Cell1, Cell2: TCell; var Compare: Integer); begin if (Cell1.AsString = 'bigger') and (Cell2.AsString = 'smaller') then Compare := 1 else if (Cell1.AsString = 'smaller') and (Cell2.AsString = 'bigger') then Compare := -1 else Compare := 0; end;
In this example we assume that Column is filled with values: "bigger" and "smaller".
As you may see, we will simply say that value "bigger" will be always "higher" than value "smaller".
Full Example
First example is not good when there are 3 or more values to sort. When we have more than 2 values inside cells, using Index function is better solution.
We will need to create following function:
function GetIndex(s: string): Integer; begin // insert logic here end;
This function will simply return Index for some value inside cell. We will compare Index of first Cell with index of second Cell and set Compare parameter:
procedure TForm1.NextGrid1Compare(Sender: TObject; Cell1, Cell2: TCell; var Compare: Integer); begin if GetIndex(Cell1.AsString) > GetIndex(Cell2.AsString) then Compare := 1 else if GetIndex(Cell1.AsString) < GetIndex(Cell2.AsString) then Compare := -1 else Compare := 0; end;
Now all we need is to add code to our GetIndex function. In our example, we will sort Planets names by their sizes:
implementation var Planets: array[1..9] of string = ('pluto', 'mercury', 'mars', 'venus', 'earth', 'neptune', 'uranus', 'saturn', 'jupiter');
Here we have define one Array with all planets. Please note that we have arrange Planets inside this array from smallest to largest.
Now we only need to assign size Index to each planet. We will made this inside GetIndex function:
function GetIndex(s: string): Integer; var i: Integer; begin s := LowerCase(s); Result := 1; for i := 1 to High(Planets) do begin if s = Planets[i] then begin Result := i; Exit; end; end; end;
With using custom sorting you can sort column by any criteria you need, such as sorting textual column with non-english Alphabets.
We recommend downloading demo from this page.
Comments
2008-06-05 07:01:43
It'll be nice if in the event is included info ot that if the comparing will be ascending or descending.
2008-06-05 08:35:58
When you click on a column second time the OnCompare event is not fired so I cannot do my custom sort algorithm. I'm having a grid like that
Text Group
a 1
b 1
c 1
a 2
b 2
c 2
I want to do a custom sort on the first column, but always keep the second column in the same order. But that's not possible with your current implementation, which I support calls some method to inverse fill the grid view if the column is already sorted and the user clicks again on that column.
Text Group
a 1
b 1
c 1
a 2
b 2
c 2
I want to do a custom sort on the first column, but always keep the second column in the same order. But that's not possible with your current implementation, which I support calls some method to inverse fill the grid view if the column is already sorted and the user clicks again on that column.
2008-06-07 14:11:02
It'll be nice if in the event is included info ot that if the comparing will be ascending or descending.
Descending/Ascending may be readed from SortKind property of Column.
I want to do a custom sort on the first column, but always keep the second column in the same order. But that's not possible with your current implementation, which I support calls some method to inverse fill the grid view if the column is already sorted and the user clicks again on that column.
coFullResort flag in Options property of column will be added in upcoming release.
Best regards
Boki (BergSoft)
Descending/Ascending may be readed from SortKind property of Column.
I want to do a custom sort on the first column, but always keep the second column in the same order. But that's not possible with your current implementation, which I support calls some method to inverse fill the grid view if the column is already sorted and the user clicks again on that column.
coFullResort flag in Options property of column will be added in upcoming release.
Best regards
Boki (BergSoft)