Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 6 of 6 total
Thread Object Array Sort
Thu, Mar 10 2016 10:52 AMPermanent Link

Mark Brooks

Slikware

Avatar

Tim

Is there, buried deep inside EWB, any code that'll expedite my sorting an array of objects? I'm thinking some Quicksort or similar that calls a comparison function?

Thanks
Mark
Thu, Mar 10 2016 2:34 PMPermanent Link

Raul

Team Elevate Team Elevate

<<Is there, buried deep inside EWB, any code that'll expedite my sorting an array of objects? I'm thinking some Quicksort or similar that calls a comparison function?
>>

There is a TObjectList class - have not tried to use myself but i would either use it for your object storage since it has sorting already built-in or look at it's code. It's in webcore.

Raul
Fri, Mar 11 2016 7:58 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Mark,

<< Is there, buried deep inside EWB, any code that'll expedite my sorting an array of objects? I'm thinking some Quicksort or similar that calls a comparison function? >>

In addition to what Raul said, you'll need to create a descendant of the TObjectList to implement these methods for comparing the objects:

        function SortCompare(L,R: TObject): Integer; virtual;
        function SortCompare(L: TObject; const R: String): Integer; virtual;

The first one is mandatory for sorting, while the second is used to search for an object by "name" using this method:

http://www.elevatesoft.com/manual?action=viewmethod&id=ewb2&comp=TObjectList&method=Find

Tim Young
Elevate Software
www.elevatesoft.com
Fri, Mar 11 2016 8:39 AMPermanent Link

Mark Brooks

Slikware

Avatar

Raul / Tim

Thanks for your input, but I'm a little stuck with an array of objects since this is the object model I'm using to handle arrays of JSON objects returned from a REST API. Otherwise i would use the TObjectList.

The API response object looks like this:

 TCastrumDocumentResponse = class(TCastrumBaseResponse)
 private
   fDocument: TCastrumDocument;
   fDocuments: array of TCastrumDocument;
 protected
   function LoadProperty(AReader: TReader): Boolean; override;
   function LoadArrayElement(AReader: TReader): Boolean; override;
 published
   property Document: TCastrumDocument read fDocument;
   property Documents: array of TCastrumDocument read fDocuments;
 public
   constructor Create; override;
   destructor Destroy; override;
 end;

Then the LoadProperty is overridden to handle the array:

function TCastrumDocumentResponse.LoadProperty(AReader: TReader): Boolean;
begin
 if SameText(AReader.GetPropertyName,'Documents') then
   begin
     Result := True;
     AReader.SkipPropertyName;
     AReader.SkipPropertySeparator;
     SetLength(fDocuments,0);
     LoadArray(AReader);
   end
 else
   Result := inherited LoadProperty(AReader);
end;

Then the LoadArrayElement is overridden to handle each element:

function TCastrumDocumentResponse.LoadArrayElement(AReader: TReader): Boolean;
var
 D: TCastrumDocument;
begin
 D := TCastrumDocument.Create;
 try
   D.LoadObject(AReader);
   SetLength(fDocuments,Length(fDocuments) + 1);
   fDocuments[Length(fDocuments) - 1] := D;
   Result := True;
 except
   D.Free;
   raise;
 end;
end;

So I end up with a response object that contains an array of TCastrumDocument instances that are, going back to the initial question, not sorted correctly!

Make sense?
Mon, Mar 14 2016 8:20 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Mark,

<< So I end up with a response object that contains an array of TCastrumDocument instances that are, going back to the initial question, not sorted correctly! >>

The code for sorting an array is in the TObjectList class, so you can grab it from there and put it in your own function.   Then, just call it after you call LoadArray.

Tim Young
Elevate Software
www.elevatesoft.com
Mon, Mar 14 2016 9:04 AMPermanent Link

Mark Brooks

Slikware

Avatar

Tim Young [Elevate Software] wrote:

>>The code for sorting an array is in the TObjectList class, so you can grab it from there and put it in your own >>function.   Then, just call it after you call LoadArray.

Got it - thanks Tim
Image