You are not logged in.
I bring some records from the database with a specific sort order and I put it in a record type vector. I need to add more items to this vector by keeping the initial ordering, however the records that are added are left at the end of the vector.
I want to know if there is any way to reorder this vector in the same way as it does in SQL ORDER BY.
(Sorry for my English)
Offline
For more than one column/field?
Like this: "ORDER BY NAME, PRIORITY, DATEINI ASC"
I was able to do only with one column
Last edited by wendel (2017-05-30 21:19:30)
Offline
I resolved passing this function to the Sort:
function OrderBy(const A, B): Integer;
function SortDateTime(const A, B): Integer;
begin
if TDateTime(A) < TDateTime(B) then
Result := -1
else
if TDateTime(A) > TDateTime(B) then
Result := 1
else
Result := 0;
end;
begin
Result := SortDynArrayInteger(TRec(A).USER_ID, TRec(B).CUSER_ID);
if Result = 0 then
begin
Result := SortDynArrayInteger(TRec(A).PRIORITY_ID, TRec(B).PRIORITY_ID);
if Result = 0 then
Result := SortDateTime(TRec(A).DATEINI, TRec(B).DATEINI);
end;
end;
Offline
Another way to write it:
function OrderBy(const A,B): integer;
var ra: TRec absolute A;
rb: TRec absolute B;
begin
result := ra.USER_ID - rb.USER_ID;
if result <> 0 then
exit;
result := ra.PRIORITY_ID - rb.PRIORITY_ID;
if result <> 0 then
exit;
if ra.DATEINI < rb.DATEINI then
result := -1 else
if ra.DATEINI > rb.DATEINI then
result := 1;
end;
The same comparison, but slightly faster, and perhaps easier to read/maintain.
Offline