Custom Sorting on Segments/Smart Segments

:exclamation:I’m not sure if this is a bug or a feature request. Please advise.

Feature(s) impacted

This affects Segments/Smart Segments.

Observed behavior

When inputting data into a Segment or a Smart Segment, the sorting is lost when creating the segment.

Expected behavior

What I assumed would be happening is that the Id’s being passed through to the segment would retain their sorting. (I see from the Forest Admin dashboard you are only allowed to select columns from the table to sort by default.) Using the code below we get an array of Ids that we would like to stay in the same order. The same should apply using a normal Segment through the Forest Dashboard.

const segments: SmartSegmentOptions[] = [
        name: 'NTUs.',
        where: juristicEntity => {
            const result = await juristicEntityRepository.Query<JuristicEntity>(`
                      SELECT "Applications"."JuristicEntityId" as "Id"
                      FROM "Applications"
                      WHERE "Applications"."Approved" = TRUE
                      AND NOT "Applications"."JuristicEntityId" IN (SELECT DISTINCT "FinanceAgreements"."JuristicEntityId"
                      FROM "FinanceAgreements" WHERE "FinanceAgreements"."Status" IN ('Complete', 'InRepayment'))
                      group by "Applications"."JuristicEntityId"
                      order by min("Applications"."ApprovedOn") desc;
            const ids = => je.Id);
            return { Id: { []: ids } };

Liana.collection('DbJuristicEntity', {

Is it currently possible to have sorting using a custom query for a segment or have I done something incorrectly?


  • Project name: Bridgement.1
  • Team name: Operations and Software Dev
  • Environment name: Production
  • Agent type & version: “forest-express-sequelize”: “^8.4.10”,
  • Recent changes made on your end if any: N/A

Hey @SoftwareBridgementKe :wave:

I just did a quick and simple test on my end, and indeed, I’m able to reproduce this behavior.
By default, we’re sorting on id, but even disabling this behavior seems to re-order the list, no matter what. However, I’m not sure from where this behavior comes from, so I’ll dig a little further to see if I’m able to find a workaround for your use-case.

I’ll let you know if I’m able to spot something, or if I push this as a feature request on our product board.

1 Like

@SoftwareBridgementKe :wave:

It looks like the records are re-ordered no matter what I try :confused:

I’ll push this as a product board suggestion, and switch the topic category accordingly.

The only workaround, far from being ideal but would work, would be to add a smart field with the matching aggregation value, and implement sorting using this smart field value to make sure the records are always in the right order. Don’t hesitate to open a new thread if you were having issue while giving a try to this solution, if you feel to.

Hi, that would be a great feature if it gets added.

In the meantime I’m following this documentation: Sort by smart field - Developer guide and I noticed for Typescript there is no isSortable on the SmartFieldOptions interface when I define the smart field.