--- E:/bds4/Imports/jvcl/run/__history/JvMemoryDataset.pas.~1~	Wed Nov 29 19:13:00 2006
+++ E:/bds4/Imports/jvcl/run/JvMemoryDataset.pas	Thu Sep 13 13:50:32 2007
@@ -130,7 +130,7 @@
     function AddRecord: TJvMemoryRecord;
     function InsertRecord(Index: Integer): TJvMemoryRecord;
     function FindRecordID(ID: Integer): TJvMemoryRecord;
-    procedure CreateIndexList(const FieldNames: string);
+    procedure CreateIndexList(const FieldNames: WideString); 
     procedure FreeIndexList;
     procedure QuickSort(L, R: Integer; Compare: TCompareRecords);
     procedure Sort;
@@ -1960,6 +1960,10 @@
 procedure TJvMemoryData.SortOnFields(const FieldNames: string = '';
   CaseInsensitive: Boolean = True; Descending: Boolean = False);
 begin 
+  // Post the table before sorting
+  if State in dsEditModes then
+    Post;
+
   if FieldNames <> '' then
   	CreateIndexList(FieldNames)
   else
@@ -2032,6 +2036,7 @@
 function TJvMemoryData.CompareRecords(Item1, Item2: TJvMemoryRecord): Integer;
 var
   Data1, Data2: PChar;
+  CData1, CData2, Buffer1, Buffer2: array[0..dsMaxStringSize] of Char;
   F: TField;
   I: Integer;
 begin
@@ -2041,24 +2046,50 @@
     for I := 0 to FIndexList.Count - 1 do
     begin
       F := TField(FIndexList[I]);
+      if F.FieldKind = fkData then
+      begin
       Data1 := FindFieldData(Item1.Data, F);
       if Data1 <> nil then
       begin
         Data2 := FindFieldData(Item2.Data, F);
         if Data2 <> nil then
         begin
-          if (Data1[0] <> #0) and (Data2[0] <> #0) then
+            if Boolean(Data1[0]) and Boolean(Data2[0]) then
           begin
             Inc(Data1);
             Inc(Data2);
             Result := CompareFields(Data1, Data2, F.DataType,
               FCaseInsensitiveSort);
           end
-          else
-          if Data1[0] <> #0 then
+            else if Boolean(Data1[0]) then
             Result := 1
+            else if Boolean(Data2[0]) then
+              Result := -1;
+            if FDescendingSort then
+              Result := -Result;
+          end;
+        end;
+        if Result <> 0 then
+          Exit;
+      end
           else
-          if Data2[0] <> #0 then
+      begin
+        FillChar(Buffer1, dsMaxStringSize, #0);
+        FillChar(Buffer2, dsMaxStringSize, #0);
+        RecordToBuffer(Item1, Buffer1);
+        RecordToBuffer(Item2, Buffer2);
+        Move(Buffer1[1 + FRecordSize + F.Offset], CData1, F.DataSize);
+        if CData1 <> nil then
+        begin
+          Move(Buffer2[1 + FRecordSize + F.Offset], CData2, F.DataSize);
+          if CData2 <> nil then
+          begin
+            if Boolean(CData1[0]) and Boolean(CData2[0]) then
+              Result := CompareFields(@CData1, @CData2, F.DataType,
+                FCaseInsensitiveSort)
+            else if Boolean(CData1[0]) then
+              Result := 1
+            else if Boolean(CData2[0]) then
             Result := -1;
           if FDescendingSort then
             Result := -Result;
@@ -2068,12 +2099,12 @@
         Exit;
     end;
   end;
+  end;
   if Result = 0 then
   begin
     if Item1.ID > Item2.ID then
       Result := 1
-    else
-    if Item1.ID < Item2.ID then
+    else if Item1.ID < Item2.ID then
       Result := -1;
     if FDescendingSort then
       Result := -Result;
@@ -2088,7 +2119,7 @@
     Result := False;
 end;
 
-procedure TJvMemoryData.CreateIndexList(const FieldNames: string);
+procedure CreateIndexList(const FieldNames: WideString);
 var
   Pos: Integer;
   F: TField;
@@ -2101,7 +2132,7 @@
   while Pos <= Length(FieldNames) do
   begin
     F := FieldByName(ExtractFieldNameEx(FieldNames, Pos));
-    if (F.FieldKind = fkData) and
+    if {(F.FieldKind = fkData) and} 
       (F.DataType in ftSupported - ftBlobTypes) then
       FIndexList.Add(F)
     else
