# How to sort by multiple fields in a formula

**URL:** <https://community.fibery.io/t/how-to-sort-by-multiple-fields-in-a-formula/5389>\
**Category:** Get Help\
**Created:** [November 8, 2023, 4:49pm UTC](https://community.fibery.io/t/how-to-sort-by-multiple-fields-in-a-formula/5389 "2023-11-08T16:49:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![carson](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/carson/32/7503_2.png) [@carson](https://community.fibery.io/u/carson)\
**Post date:** [November 8, 2023, 4:49pm UTC](https://community.fibery.io/t/how-to-sort-by-multiple-fields-in-a-formula/5389/1 "2023-11-08T16:49:13Z")

</div>

Is it currently possible to sort by multiple fields in a formula, like it is in the UI?

The scenario is just the common situation of sorting semver strings (1.23.4-rc2) correctly. There’s trickiness in sorting the number portions correctly but I think I can figure out a way around that. However, if we’re also wanting to sort the suffix single-selects (-dev, -alpha, -beta, -rc#) by an internal value so they show up in the right order, the easiest thing to do is just to sort by multiple fields.

I can see on the fibery formula reference ([. | Fibery](https://the.fibery.io/@public/User_Guide/Guide/Formulas-Reference-44/anchor=Sort(-field,-isAscending-)-+-First()-and--e854f229-3f1e-4ca7-9f49-1901823c5286)) that the syntax for the sort function apparently is able to receive an array `Sort([field, isAscending])`? Maybe the presence of the brackets around both parameters is a typo, because when I attempt to do this in the web UI formula editor, it errors out because it doesn’t recognize the array syntax. You also can’t daisy chain multiple .Sort().Sort().Sort() statements…

Anybody have any ideas?

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [November 8, 2023, 4:59pm UTC](https://community.fibery.io/t/how-to-sort-by-multiple-fields-in-a-formula/5389/2 "2023-11-08T16:59:29Z")

</div>

> [@carson](#):
>
> Maybe the presence of the brackets around both parameters is a typo, because when I attempt to do this in the web UI formula editor, it errors out

In this instance, the brackets indicate that the parameters are optional, not that you should pass an array.

> [@carson](#):
>
> You also can’t daisy chain multiple .Sort().Sort().Sort() statements…

Correct. Multiple levels of sorting in formulas are not supported.  
Note: `Sort()` in formulas has no effect on the UI - it is only usable when followed by `First()` or `Last()`

What is your ultimate goal with the formula?

---

<div class="post-metadata">

**Author:** ![carson](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/carson/32/7503_2.png) [@carson](https://community.fibery.io/u/carson)\
**Post date:** [November 8, 2023, 5:06pm UTC](https://community.fibery.io/t/how-to-sort-by-multiple-fields-in-a-formula/5389/3 "2023-11-08T17:06:59Z")

</div>

> [@Chr1sG](#):
>
> In this instance, the brackets indicate that the parameters are optional, not that you should pass an array.

Ok, I thought that might also be the case. So, not a typo ✔

> [@Chr1sG](#):
>
> What is your ultimate goal with the formula?

To correctly sort Versions in a list, and to be able to obtain the latest Version of a Product. I have Products that have multiple Versions. Versions have number fields [Major Version Number], [Minor Version Number], [Patch Version Number], and a single-select dropdown of “Version Type” which is “Development”, “Alpha”, “Beta”, “Release Candidate”, and “Production”. Each of those Version Type options has an internal integer that can be used to sort them so that e.g. “Development” is less than “Production”.

Sorting alphabetically on a formula string or automation-generated string field that concatenates these fields (e.g. “2.10.3-beta”) probably doesn’t give the desired results because alphabetical sort functions usually consider “2.10.0” greater than “2.2.0” because ASCII “0” is greater than the ASCII “.”

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [November 8, 2023, 6:44pm UTC](https://community.fibery.io/t/how-to-sort-by-multiple-fields-in-a-formula/5389/4 "2023-11-08T18:44:21Z")

</div>

@carson  
You could create a text Formula field that defines the “Sort Text” of each entity, and sort on that field.

Maybe a Formula like:

```auto
Right("00000000" + ToText([Major Version Number]), 8) + "." +
Right("00000000" + ToText([Minor Version Number]), 8) + "." +
Right("00000000" + ToText([Patch Version Number]), 8) + "." 

```
