A developer wrote the following query to create a list of activities sorted by activity pattern, and then returns the first activity for a failed policy bind:

This query uses the sortBy() and firstwhere() methods which are anti-patterns. Where should the developer handle the filtering and sorting to follow best practices?
Correct Answer: C
In Guidewire InsuranceSuite development, one of the most critical performance principles is "database-first" processing. When using the GosuQuery API, developers have two ways to manipulate data: at theDatabase level(via the Query object) or at theApplication Server level(via the Result/Collection object).
Methods like sortBy() and firstWhere() are part of the Gosu collection library. When applied to a query result, these methods trigger the execution of the SQL query, fetchallmatching records from the database into the application server's memory, and then perform the sorting and filtering in the Java/Gosu heap. This is a significant anti-pattern because it consumes excessive memory and CPU cycles on the application server, especially if the underlying table (like Activity) contains thousands or millions of rows.
According to best practices, the developer should handle filtering and sortingin the database(Option C). This is achieved by using the compare() and orderBy() methods directly on the Query object before calling .select().
By doing so, Guidewire generates a SQL statement with a WHERE clause for filtering and an ORDER BY clause for sorting. The database engine, which is highly optimized for these operations, then returns only the specific record needed. For the "first" record requirement, the developer should combine the database-level orderBy() with a getFirstResult() call on the result set. This ensures that only the minimal required data is transferred over the network and loaded into memory, maintaining high application throughput and preventing
"OutOfMemory" errors.