# How to access entities' "natural sort order" in Rules/Buttons/scripts?

**URL:** <https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967>\
**Category:** Get Help\
**Created:** [August 31, 2021, 10:17pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967 "2021-08-31T22:17:04Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [August 31, 2021, 10:17pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/1 "2021-08-31T22:17:04Z")

</div>

Entities can be (re)ordered manually via dragging (i.e. in a Card or List view).  
How can such entities’ relative order be determined or set in a Rule/Button/script?

---

<div class="post-metadata">

**Author:** ![Polina\_Zenevich](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/polina_zenevich/32/480_2.png) [@Polina\_Zenevich](https://community.fibery.io/u/Polina_Zenevich)\
**Post date:** [September 7, 2021, 9:42am UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/2 "2021-09-07T09:42:52Z")

</div>

Hi, Matt!  
While we’re thinking about the answer, would be glad to hear more details about your use case, really curious about it))))

---

<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:** [January 13, 2022, 5:22pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/3 "2022-01-13T17:22:12Z")

</div>

Example use case:

I have a hierarchical DB of “web pages”. I manually order (by dragging) these to define the desired order of the pages within the website navigation menu.

I want to create a Button that will read this “natural sort order” and generate a `bash` function to effect this ordering within the website’s WordPress menu (e.g., `wp cli` commands`).

---

<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:** [January 13, 2022, 9:39pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/4 "2022-01-13T21:39:12Z")

</div>

If you just need to read the sort order, then it is available via a (hidden) field called ‘Rank’ which is a number that determines the ordering (loweest number is highest up the list).

`const entityWithExtraFields = await fibery.getEntityById(entity.type, entity.id, ['Rank']);`

However the numbers are not simple, single-digit integers. For example, my 5 tasks have the following values:  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/c/ca8d923d6a26902ae1f2ee499c45f0f58bf56b80.png)

I don’t know how useful this is 🤔

---

<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:** [January 13, 2022, 9:45pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/5 "2022-01-13T21:45:46Z")

</div>

I think that would work fine - thanks!

---

<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:** [February 3, 2022, 11:00pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/6 "2022-02-03T23:00:36Z")

</div>

Does the API also allow us to set a new value for `Rank`?

---

<div class="post-metadata">

**Author:** ![Sergey\_Truhtanov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sergey_truhtanov/32/13986_2.png) [@Sergey\_Truhtanov](https://community.fibery.io/u/Sergey_Truhtanov)\
**Post date:** [February 4, 2022, 9:49am UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/7 "2022-02-04T09:49:02Z")

</div>

Hello. Yes, api allows that. You should be a bit careful though.

For example if you have an entity with rank 1000 and to insert entity after it you will set rank 1001 to new one, then on UI you will get rank-recalculation process started right after d&d between those cards, as 1 is the minimum precision we have. Rank recalculation will prevent you from d&d cards until it is finished. It will take some time depending on the number of entities in your workspace.

On UI when inserting a card between two, we just generate rank as the middle value in between of their ranks.

And what is your case here? Could you please describe it a bit…

---

<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:** [February 4, 2022, 4:48pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/8 "2022-02-04T16:48:19Z")

</div>

> [@Sergey\_Truhtanov](#):
>
> And what is your case here? Could you please describe it a bit…

I have a DB of “web pages”, a self-referencing hierarchy of parent and child page entities.  
Essentially I want to show a Table view of these pages, ordered like a Hierarchical List view.  
The script would sort the pages into their correct order as they would appear in a Menu on the website.

Like this:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/4/4b508a9d5fb5f611c08397a4341d2e9079dda7d1.png)

**Of course the better solution would be to implement such a hierarchical sort in Table view itself.**

List view is not ideal because it does not align the columns, nor allow editing like Table view.

---

<div class="post-metadata">

**Author:** ![Sergey\_Truhtanov](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/sergey_truhtanov/32/13986_2.png) [@Sergey\_Truhtanov](https://community.fibery.io/u/Sergey_Truhtanov)\
**Post date:** [February 8, 2022, 12:16pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/9 "2022-02-08T12:16:05Z")

</div>

If you don’t cross-prioritize those “web pages” with entities from other databases, I think it is totally ok to set ranks programmatically. I suggest to use 1000000 as a step between entities ranks, at least we use this value on UI. So you may take Number.MIN\_SAFE\_INTEGER as a beginning and add taht step each time.

Will require some javascript to complete your task of course…

---

<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:** [February 8, 2022, 1:29pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/10 "2022-02-08T13:29:27Z")

</div>

Looking at your screen grab, wouldn’t sorting by Path achieve what you want?

---

<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:** [February 8, 2022, 7:28pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/11 "2022-02-08T19:28:27Z")

</div>

In this case, if everything is wanted in alphabetical order, yes. But the more general question is how a script can read or change the ordering of entities that may have been _manually reordered_ (as page order within a menu usually is).

---

<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:** [February 8, 2022, 10:02pm UTC](https://community.fibery.io/t/how-to-access-entities-natural-sort-order-in-rules-buttons-scripts/1967/12 "2022-02-08T22:02:29Z")

</div>

Yep, if you need to read the manually set order, then using the API to access Rank is the way to go.  
FWIW we are planning to make improvements to Table View so that it combines some of the best bits of List View whilst retaining the classic features (like column alignment) but I have no idea of the ETA I’m afraid.
