# Problem with sorting on a related Foundset

**URL:** <https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656>\
**Category:** Classic Servoy\
**Created:** [January 28, 2008, 2:04pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656 "2008-01-28T14:04:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ars](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@ars](https://forum.servoy.com/u/ars)\
**Post date:** [January 28, 2008, 2:04pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/1 "2008-01-28T14:04:55Z")

</div>

Hi all,

I am sorting a related Foundset based on a Field(Integer).

```auto
tbl_a_to_tbl_b.sort('tbl_a_to_tbl_b.company_id asc');

```

**tbl\_a\_to\_tbl\_b** - Is a Relation

I am displaying all the related records in a Table View.  
But when I am executing the above code it is giving the wrong order. But when I am clicking the Header of the Field(company\_id), It is giving the correct result.

But the correct sort of records can’t be achieved by the above code.

Am I missing some thing?

Is there any work around?

---

<div class="post-metadata">

**Author:** ![ars](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@ars](https://forum.servoy.com/u/ars)\
**Post date:** [January 28, 2008, 2:16pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/2 "2008-01-28T14:16:29Z")

</div>

Rather It is working If I am sorting on the table on which the Field (company\_id) is based upon.

```auto
forms.tbl_b.controller.sort('company_id asc');

```

Can’t be the columns are sortable over a relation?

---

<div class="post-metadata">

**Author:** ![Joas](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/joas/32/5095_2.png) [@Joas](https://forum.servoy.com/u/Joas)\
**Post date:** [January 28, 2008, 3:03pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/3 "2008-01-28T15:03:58Z")

</div>

If you use [relation].sort(), you’re already working through the relation, so you don’t need to specify it again when addressing the field names:

```auto
tbl_a_to_tbl_b.sort('company_id asc');

```

---

<div class="post-metadata">

**Author:** ![Robert\_Huber](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Robert\_Huber](https://forum.servoy.com/u/Robert_Huber)\
**Post date:** [January 28, 2008, 5:27pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/4 "2008-01-28T17:27:52Z")

</div>

Hi Joas, Ars

I have the same problem. I can’t get a column sorted by a relationship!

```auto
connections_departing_place_terminated_by_stations.sort('name asc');

```

Joas

I have a second problem: I like to make an initial sort. I would like to sort the same attribut (name) via 2 different relationships. In the Property Editor window, when double clicking on the InitialSort property on the forms, the relationship is NOT shown, only the attribute. If I like to make an initial sort like

connections\_departing\_place\_terminated\_by\_stations.sort(‘name asc’);  
connections\_arriving\_place\_terminated\_by\_stations.sort(‘name asc’);

It just doesn’t work! I assume it’s a bug, is it?

Best regards, Robert

---

<div class="post-metadata">

**Author:** ![Robert\_Huber](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Robert\_Huber](https://forum.servoy.com/u/Robert_Huber)\
**Post date:** [January 28, 2008, 5:53pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/5 "2008-01-28T17:53:27Z")

</div>

Joas

First part is the wrong code for what I wanted to do.

Correct code is:

```auto
controller.sort('connections_departing_place_terminated_by_stations.name');

```

It also works for the same attribute via 2 relationships (what I would like to do in Initial Sort!)

Code then looks like this:

```auto
controller.sort('connections_departing_place_terminated_by_stations.name, connections_arriving_place_terminated_by_stations.name');

```

Initial Sort remains a problem. How to do in Initial Sort what I achieve with the above code?

Best regards, Robert

---

<div class="post-metadata">

**Author:** ![ars](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@ars](https://forum.servoy.com/u/ars)\
**Post date:** [January 29, 2008, 5:19am UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/6 "2008-01-29T05:19:51Z")

</div>

Hi Joas,

> Joas:  
> If you use [relation].sort(), you’re already working through the relation, so you don’t need to specify it again when addressing the field names:
> 
> ```auto
> tbl_a_to_tbl_b.sort('tbl_a_to_tbl_b.company_id asc');
> 
> ```

Yep, I have the relation, but i want to sort the related records(filtered by the same relationship) based on a Field of the table on which the relationship is based upon.

By using the below code.

```auto
tbl_a_to_tbl_b.sort('tbl_a_to_tbl_b.company_id asc');

```

But, It is not sorting the record ![:(]( "Sad") .

Relationship is from the Table : **tbl\_a** to Table : **tbl\_b**  
The Field(company\_id), on which i want to sort the records, is from **tbl\_b**.

Hope, it is clear to you guys.

How can I do the same, or it is a Bug?

---

<div class="post-metadata">

**Author:** ![ars](https://avatars.discourse-cdn.com/v4/letter/a/e19adc/32.png) [@ars](https://forum.servoy.com/u/ars)\
**Post date:** [January 29, 2008, 2:01pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/7 "2008-01-29T14:01:55Z")

</div>

Aah, that will work if we sort the real field not through the related Field. ![:)]( "Smile")

This will solve the issue.

```auto
tbl_a_to_tbl_b.sort('company_id asc');

```

---

<div class="post-metadata">

**Author:** ![Joas](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/joas/32/5095_2.png) [@Joas](https://forum.servoy.com/u/Joas)\
**Post date:** [January 30, 2008, 8:54am UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/8 "2008-01-30T08:54:36Z")

</div>

> ars:  
> Aah, that will work if we sort the real field not through the related Field. ![:)]( "Smile")
> 
> This will solve the issue.
> 
> ```auto
> tbl_a_to_tbl_b.sort('company_id asc');
> 
> ```

That’s what I was telling you, but you edited my post when quoting ![:)]( "Smile")

> Robert Huber:  
> Initial Sort remains a problem. How to do in Initial Sort what I achieve with the above code?

It is true that you can’t select the same field of the same table more than once in the initialSort dialog. You should do a feature request if you want this.  
For now you can execute your code in the onLoad method to get the same result.

---

<div class="post-metadata">

**Author:** ![Robert\_Huber](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Robert\_Huber](https://forum.servoy.com/u/Robert_Huber)\
**Post date:** [January 30, 2008, 1:32pm UTC](https://forum.servoy.com/t/problem-with-sorting-on-a-related-foundset/8656/9 "2008-01-30T13:32:34Z")

</div>

Hi Joas

Yes, I need that functionality, so I will make a feature request. The use of an onLoad method is as you say for now a workaround.

Thanks and best regards, Robert

> Robert Huber:  
> Initial Sort remains a problem. How to do in Initial Sort what I achieve with the above code?

It is true that you can’t select the same field of the same table more than once in the initialSort dialog. You should do a feature request if you want this.  
For now you can execute your code in the onLoad method to get the same result.[/quote]
