Login ProductsSalesSupportDownloadsAbout |
Home » Technical Support » Elevate Web Builder Technical Support » Support Forums » Elevate Web Builder General » View Thread |
Messages 1 to 6 of 6 total |
Object Array Sort |
Thu, Mar 10 2016 10:52 AM | Permanent Link |
Mark Brooks Slikware | 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 PM | Permanent Link |
Raul 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 AM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. 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 AM | Permanent Link |
Mark Brooks Slikware | 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 AM | Permanent Link |
Tim Young [Elevate Software] Elevate Software, Inc. 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 AM | Permanent Link |
Mark Brooks Slikware | 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 |
This web page was last updated on Wednesday, October 9, 2024 at 05:37 AM | Privacy PolicySite Map © 2024 Elevate Software, Inc. All Rights Reserved Questions or comments ? E-mail us at info@elevatesoft.com |