# Custom Sorting on Segments/Smart Segments

**URL:** <https://community.forestadmin.com/t/custom-sorting-on-segments-smart-segments/4305>\
**Category:** Feature suggestions\
**Created:** [March 9, 2022, 7:19am UTC](https://community.forestadmin.com/t/custom-sorting-on-segments-smart-segments/4305 "2022-03-09T07:19:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SoftwareBridgementKe](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/softwarebridgementke/32/3066_2.png) [@SoftwareBridgementKe](https://community.forestadmin.com/u/SoftwareBridgementKe)\
**Post date:** [March 9, 2022, 7:19am UTC](https://community.forestadmin.com/t/custom-sorting-on-segments-smart-segments/4305/1 "2022-03-09T07:19:55Z")

</div>

> ❗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.

```javascript
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 = result.map(je => je.Id);
            return { Id: { [Op.in]: ids } };
        }
    }
];

Liana.collection('DbJuristicEntity', {
    segments
});

```

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

## Context

- 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

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [March 9, 2022, 11:52am UTC](https://community.forestadmin.com/t/custom-sorting-on-segments-smart-segments/4305/2 "2022-03-09T11:52:27Z")

</div>

Hey @SoftwareBridgementKe 👋

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.

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [March 11, 2022, 10:50am UTC](https://community.forestadmin.com/t/custom-sorting-on-segments-smart-segments/4305/3 "2022-03-11T10:50:44Z")

</div>

@SoftwareBridgementKe 👋

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

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.

---

<div class="post-metadata">

**Author:** ![SoftwareBridgementKe](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/softwarebridgementke/32/3066_2.png) [@SoftwareBridgementKe](https://community.forestadmin.com/u/SoftwareBridgementKe)\
**Post date:** [March 11, 2022, 12:09pm UTC](https://community.forestadmin.com/t/custom-sorting-on-segments-smart-segments/4305/4 "2022-03-11T12:09:25Z")

</div>

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](https://docs.forestadmin.com/documentation/reference-guide/smart-fields/smart-field-examples/sort-by-smart-field) and I noticed for Typescript there is no `isSortable` on the `SmartFieldOptions` interface when I define the smart field.
