#1 2017-05-30 19:11:25

wendel
Member
From: Mato Grosso, Brasil
Registered: 2017-02-13
Posts: 4
Website

Is there any way to simulate an ORDER BY for a TDynArray?

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

#2 2017-05-30 21:00:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Is there any way to simulate an ORDER BY for a TDynArray?

You can sort the TDynArray, I suppose.

Offline

#3 2017-05-30 21:08:18

wendel
Member
From: Mato Grosso, Brasil
Registered: 2017-02-13
Posts: 4
Website

Re: Is there any way to simulate an ORDER BY for a TDynArray?

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

#4 2017-05-31 05:54:48

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Is there any way to simulate an ORDER BY for a TDynArray?

You can supply the comparison function used for sorting, so you can specify more than one column.

Offline

#5 2017-05-31 17:35:08

wendel
Member
From: Mato Grosso, Brasil
Registered: 2017-02-13
Posts: 4
Website

Re: Is there any way to simulate an ORDER BY for a TDynArray?

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

#6 2017-06-01 06:09:16

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: Is there any way to simulate an ORDER BY for a TDynArray?

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

#7 2017-06-02 13:07:45

wendel
Member
From: Mato Grosso, Brasil
Registered: 2017-02-13
Posts: 4
Website

Re: Is there any way to simulate an ORDER BY for a TDynArray?

It worked very well. Thank you! smile

Offline

Board footer

Powered by FluxBB