# Sort a Joined List using a secondary joined list as sort keys

**URL:** <https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412>\
**Category:** Ask for Help\
**Tags:** custom-code\
**Created:** [August 13, 2021, 4:16am UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412 "2021-08-13T04:16:49Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 3:42pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/21 "2022-01-29T15:42:29Z")

</div>

I’m just not really good at coding…  
learned only to fork on Replit and I could find my way around changing 1 little think n the code 🙂

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 3:44pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/22 "2022-01-29T15:44:02Z")

</div>

You can fork it and do whatever you like with it 😉

But if all you want is an ascending sort, you can just override the default by typing asc in that box

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 3:44pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/23 "2022-01-29T15:44:32Z")

</div>

but then the sorted list is not correct

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 3:45pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/24 "2022-01-29T15:45:47Z")

</div>

You’d have the same problem if you changed the default. I think you need an if-then-else column

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 3:48pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/25 "2022-01-29T15:48:10Z")

</div>

how do I determine which joind list to show if the sort column is in several rows/ how do I check it exists in some\all of them?  
I forgot to mention that I only display this row in a specific viewed group.  
Its a bit hard for me to explain. but I dont use the hole table for the list.  
only in the group the user currently views.

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 4:00pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/26 "2022-01-29T16:00:28Z")

</div>

Okay, let me try to paraphrase just to confirm my understanding:

- The table contains rows for all users
- Each user may or may not have entries in the Sort column
- For each user, if _any_ of that users rows have an empty value in the Sort column, then the table should _not_ be sorted for that user

Is that correct?

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 4:04pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/27 "2022-01-29T16:04:25Z")

</div>

1 and 2 ok  
3 - It should be sorted but like the glide logic for sorting the list because i display the sorted inline list right under and the logic is:  
If all rows have no sort then its sheet order asc.  
If some rows have sort then its blank sort top and the sort by asc under them.  
If all rows have sort then its regular asc by sort

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 4:10pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/28 "2022-01-29T16:10:29Z")

</div>

I was actually able to fork and find where to change the default order. hw do I get the link for the code in Github?  
This dosnt work : [GitHub - Linalt111/glide-jl-sort-by-keys-advanced](https://github.com/Linalt111/glide-jl-sort-by-keys-advanced.git)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 4:11pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/29 "2022-01-29T16:11:53Z")

</div>

You have to publish it as Github Page - but I don’t think that’s actually going to solve your problem?

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 4:13pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/30 "2022-01-29T16:13:06Z")

</div>

> [@Darren\_Murphy](#):
>
> You have to publish it as Github Page

Is that complicated?

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 4:15pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/31 "2022-01-29T16:15:30Z")

</div>

> [@Calendar reminder](https://community.glideapps.com/t/calendar-reminder/30901/20):
>
> haha, I’ve been a Github user for many years, so wasn’t really comfortable using replit. To get these YC columns up and running using Github: Sign up for a Github account Clone the repo Publish it as a “page” To do that last step, you need to navigate to the repo, then go to Settings → Pages Everything you need to know about Github Pages:

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 4:36pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/32 "2022-01-29T16:36:14Z")

</div>

1

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 5:03pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/33 "2022-01-29T17:03:00Z")

</div>

> [@Lin\_Altshuler](#):
>
> if (sort\_keys.value === undefined) return undefined; to return the data

That won’t work, because if it just returns the data it will still contain the delimiters, which will break your HTML table.

What you could do is pass it a joined list with no delimiters if you don’t want it sorted, then you’ll be back to where you started. But that would be a bit silly. But I think you’re going down a bit of a rabbit hole here. Rather than trying to mess around with the code, I think this could be handled better in Glide, and only call the code column if it’s actually needed.

Let’s go back to this:

> [@Lin\_Altshuler](#):
>
> 1 and 2 ok  
> 3 - It should be sorted but like the glide logic for sorting the list because i display the sorted inline list right under and the logic is:  
> If all rows have no sort then its sheet order asc.  
> If some rows have sort then its blank sort top and the sort by asc under them.  
> If all rows have sort then its regular asc by sort

Firstly, I’ll assume that you have a multi-relation from your users table to this table. Given that, I’d do something like this:

- From your Users table, create a joined list through that relation using the values from the Sort column, with no separator.
- Then add an if-then-else column in your users table: If the above joined list column is not empty, then true. This will tell you whether or not the table should be sorted for each user.
- Next, add an if-then-else column in this table (the one that contains all the data): If sort is empty, then 0
- Use the above column to create the joined list of sort keys (the zeros in place of the empty values will ensure they get sorted correctly)
- Now create two template columns for the table rows - one with a separator, and one without.
- Use the experimental code column as it is, passing the joined list of table rows with the separator as the first parameter, and the joined list that uses the if-then-else column (with the zeroes) as the sort keys parameter.
- Finally, one last if-then-else column to decide which joined list to use in your table: if the if-then-else column in your Users table is true, use the sorted one, otherwise use the unsorted one.

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 5:25pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/34 "2022-01-29T17:25:27Z")

</div>

Thats really good.  
what about the half sorted lists? do I user the delimiter or not?

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 5:30pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/35 "2022-01-29T17:30:44Z")

</div>

> [@Darren\_Murphy](#):
>
> From your Users table, create a joined list through that relation using the values from the Sort column, with no separator.

Actually, just thinking about it a bit more, a better way to do this step would be to use a rollup column, taking the sum of the Sort column. If that sum is greater than zero, then it needs to be sorted.

> [@Lin\_Altshuler](#):
>
> what about the half sorted lists? do I user the delimiter or not?

Which step are you referring to here?

I think the answer is yes. Anything you send to the code column needs the delimiters. The reason for replacing the blanks with zeros is to ensure you have the correct number of sort keys. Otherwise it may not sort correctly.

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 5:36pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/36 "2022-01-29T17:36:04Z")

</div>

When sending a 0 delimited sort list it sorts it desc - reversed from the sheet order.  
that was my issue all along :).

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 5:39pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/37 "2022-01-29T17:39:04Z")

</div>

Yes, but you override that by specifying asc as the sort direction.

That won’t affect it for the ones that don’t need sorting, as they will use the unsorted joined list.

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 5:49pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/38 "2022-01-29T17:49:26Z")

</div>

> [@Darren\_Murphy](#):
>
> override that by specifying asc as the sort direction.

This is not happening - still desc whenever there is no 1\>2 the sort is reversed from the sheet order even if the sort is defined asc

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [January 29, 2022, 5:52pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/39 "2022-01-29T17:52:29Z")

</div>

Somethings wrong - can you show me?  
I need to see the values that get passed as each joined list, and the configuration on the code column.

---

<div class="post-metadata">

**Author:** ![Lin\_Altshuler](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/lin_altshuler/32/32591_2.png) [@Lin\_Altshuler](https://community.glideapps.com/u/Lin_Altshuler)\
**Post date:** [January 29, 2022, 5:53pm UTC](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412/40 "2022-01-29T17:53:38Z")

</div>

I am duplicating this app can I add you to the team?

[Previous page](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412.md?page=1)

[Next page](https://community.glideapps.com/t/sort-a-joined-list-using-a-secondary-joined-list-as-sort-keys/30412.md?page=3)
