How to sort data in MemDataset/SdfDataset/FixedFormatDataset lazarus / 16-Databases ---------------------------------------------------------------------- Brian · 2008-11-06 08:18 Does MemDataset, SdfDataset and/or FixedFormatDataset has/have indexing function such that we can show the data in sorted form? Please advise... ---------------------------------------------------------------------- vladvons · 2013-11-18 12:47 I also need field sorting in MemDataset. Please give me some direction. ---------------------------------------------------------------------- mangakissa · 2013-11-18 12:52 Indexfieldnames should sort your dataset ---------------------------------------------------------------------- vladvons · 2013-11-18 13:49 no such property 'IndexFieldNames' in TMemDataset (FPC 2.6.2) ---------------------------------------------------------------------- clauslack · 2013-11-18 14:36 I think that TMemdataset is deprecated, use TBufDataSet. http://wiki.freepascal.org/How_to_write_in-memory_database_application s_in_Lazarus/FPC#Sorting_DBGrid_on_TitleClick_event_for_TBufDataSet Regards ---------------------------------------------------------------------- mangakissa · 2013-11-18 15:09 That's not entirely true. TMemdataset is fixed in FPC 2.6.4 / FPC 2.7.1. ---------------------------------------------------------------------- LacaK · 2013-11-19 06:46 Quote from: Brian on November 06, 2008, 09:18:48 amDoes MemDataset, SdfDataset and/or FixedFormatDataset has/have indexing function such that we can show the data in sorted form? Please advise... AFAIK these datasets does not support inplace sorting/indexing. Only BufDataset (as mentioned) supports properties like IndexFieldNames, IndexName ---------------------------------------------------------------------- vladvons · 2013-11-19 20:38 Thanks BufDataset.IndexFieldNames is realy easy to use property for sorting by field. Another problem i have with BufDataset is using sort on cyrillic ftString field. DbGrid field looks like partially sorted. Should some preparation comes first or should i use some custom comparation function? ---------------------------------------------------------------------- LacaK · 2013-11-20 05:34 What encoding do you use ? (utf8?) TBufDataset expect, that string values are in ANSI codepage, because for string comparing are used AnsiCompareStr, AnsiCompareText functions ... ---------------------------------------------------------------------- vladvons · 2013-11-20 11:27 QuoteTBufDataset expect, that string values are in ANSI codepage, because for string comparing are used AnsiCompareStr, AnsiCompareText functions ... TBufDataset should expect also ftWideString Here is source part of TBufDataset: Code: [Select]procedure TCustomBufDataset.ProcessFieldCompareStruct(AField: TField; var ACompareRec : TDBCompareRec); begin   case AField.DataType of     ftString, ftFixedChar :       ACompareRec.Comparefunc := @DBCompareText;     ftWideString, ftFixedWideChar:       ACompareRec.Comparefunc := @DBCompareWideText; ... end; I defined field in BufDataset as ftWideString, but sorting still wrong visually DbGrid shows all columns correct: FileName, FileExt, FileSize numeric fields sorting is OK cyrillic string fields sorting is BAD What i do now: 1. Get files from directory (FindFirst, FindNext) 2. Add names into TStringList 3. Copy data from TStringList into TBufDataset via SysToUTF8() 4. Bind TDbGrid with TBufDataset 5. Sort Grid column by clicking Title using TBufDataset.IndexFieldNames What encoding functoins to use to adjust sorting problem? ---------------------------------------------------------------------- LacaK · 2013-11-21 07:53 Quote from: vladvons on November 20, 2013, 12:27:31 pmQuoteTBufDataset expect, that string values are in ANSI codepage, because for string comparing are used AnsiCompareStr, AnsiCompareText functions ... TBufDataset should expect also ftWideString I defined field in BufDataset as ftWideString, but sorting still wrong ... 3. Copy data from TStringList into TBufDataset via SysToUTF8() Do noy use ToUTF8 ... WideString is not expected to be UTF-8 encoded on Windows, but UTF-16 Try simple assignment to Field.AsWideString:= ... implicit conversion shoul be performed. ---------------------------------------------------------------------- vladvons · 2013-11-21 08:10 When i use Field.AsWideString conversion, then DbGrid column shows only '??????????????.txt' Code: [Select]FindNext(SearchRec); BufDataset.FieldByName('tfName').AsWideString := SearchRec.Name; ---------------------------------------------------------------------- LacaK · 2013-11-21 09:49 Yes it is problem, because lazarus expects that strings are UTF-8 encoded. So you must use any kind of hack. You need, that strings stored in WideString field in BufDataset will be in UTF-16 but when displaying in lazarus data-aware controls they must be converted to UTF-8 on the fly. It can be achieved either by hacking into TField.GetText/SetText or hacking into widestring manager. (look for  widestringmanager.Wide2AnsiMoveProc and     widestringmanager.Ansi2WideMoveProc ) Try search forums ... ---------------------------------------------------------------------- vladvons · 2013-11-21 10:41 Meantime is terrible with 'FPC AnsiString' and 'Lazarus UTF8' core. Hope once we will see native unicode in FPC too. Buffer is buffer and field may contains not only types listed in TFieldType (ftString, ftSmallint, ftInteger...etc) What about TRect comparing? It would be good to add to TBufDataset class OnCompareField property. Just don't know how to post such proposal to TBufDataset maintainers. ---------------------------------------------------------------------- LacaK · 2013-11-21 11:03 Quote from: vladvons on November 21, 2013, 11:41:48 amMeantime is terrible with 'FPC AnsiString' and 'Lazarus UTF8' core. Hope once we will see native unicode in FPC too. Yes it is problematic Quote from: vladvons on November 21, 2013, 11:41:48 amIt would be good to add to TBufDataset class OnCompareField property. Just don't know how to post such proposal to TBufDataset maintainers. You can fill bug report or you can post your proposal here, but I personally do not prefer such solution (add OnCompareField) It solves whole problem with various string encodings only partialy. IMO proper solution is add such support directly into TStringField, TWideStringField and these should perform needed translations transparently in background. ---------------------------------------------------------------------- taazz · 2013-11-21 11:12 Both are needed the OnCompare Event is needed so the user can define hes/her own comparison and sorting order and the automatic conversion between encodings  is a must too.