# How to sort a Trie.filter result?

**URL:** <https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752>\
**Category:** Developers\
**Created:** [May 15, 2021, 4:40pm UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752 "2021-05-15T16:40:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbole/32/1334_2.png) [@rbole](https://forum.dfinity.org/u/rbole)\
**Post date:** [May 15, 2021, 4:40pm UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752/1 "2021-05-15T16:40:03Z")

</div>

Hi, I would like to sort the result of a Trie.filter result.

This is my function:

> // Get all articles by category  
> public query func getArticlesByCategory (artCategory : Text) : async [ArticleCatWithId] {  
> // func filter\<K, V\>(t : Trie\<K, V\>, f : (K, V) → Bool) : Trie\<K, V\>  
> let result : Trie.Trie\<ArtId, Article\> = Trie.filter\<ArtId, Article\>(articles, func (k, v) { v.artCategory == artCategory });  
> Trie.toArray\<ArtId, Article, ArticleCatWithId\>(result, transformCategory);  
> };

Is there a way to sort the result by a property name? The title of the article e.g.?

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [May 15, 2021, 11:11pm UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752/2 "2021-05-15T23:11:20Z")

</div>

You’ll have to roll your own sort function for now, I think we couldn’t quite decide which flavour of sort we wanted to include in the base libraries.

There’s a quicksort implementation in the examples here:

> **[dfinity/examples](https://github.com/dfinity/examples/tree/master/motoko/quicksort)**
>
> master/motoko/quicksort

---

<div class="post-metadata">

**Author:** ![rbole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbole/32/1334_2.png) [@rbole](https://forum.dfinity.org/u/rbole)\
**Post date:** [May 16, 2021, 5:21am UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752/3 "2021-05-16T05:21:53Z")

</div>

Hi, thanks for pointing this out. The quick sort example is about sorting integers, so we have to adopt it.

One other solution, more easier, would be to sort the resulting array in the frontend application via Javascript.

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [May 16, 2021, 8:50am UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752/4 "2021-05-16T08:50:07Z")

</div>

Not quite. The example sorts integers but the actual function is generic and works for any type. Not sure it’s the best implementation though and not randomized as it should be.

---

<div class="post-metadata">

**Author:** ![rbole](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rbole/32/1334_2.png) [@rbole](https://forum.dfinity.org/u/rbole)\
**Post date:** [May 16, 2021, 10:55am UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752/5 "2021-05-16T10:55:06Z")

</div>

I will try it, but for most use cases the sorting would be better on the client site.

On other question regarding cycles would be, can an intensive sorting process increase the costs of running the canister?

---

<div class="post-metadata">

**Author:** ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)\
**Post date:** [May 16, 2021, 1:06pm UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752/6 "2021-05-16T13:06:00Z")

</div>

Yes. And a worse algorithm might also exceed the finite cycle budget allowed per call.

---

<div class="post-metadata">

**Author:** ![kritzcreek](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/kritzcreek/32/267_2.png) [@kritzcreek](https://forum.dfinity.org/u/kritzcreek)\
**Post date:** [May 17, 2021, 7:59am UTC](https://forum.dfinity.org/t/how-to-sort-a-trie-filter-result/3752/7 "2021-05-17T07:59:31Z")

</div>

FWIW I just added an implementation of merge sort for arrays to base: [Adds a merge-sort for Arrays by kritzcreek · Pull Request #256 · dfinity/motoko-base · GitHub](https://github.com/dfinity/motoko-base/pull/256)

Should be available with the next dfx update (or immediately with vessel if you can’t wait 😉 )
