# How to sort a list by the length of a text string?

Hi all,

I am wondering if/how you can "sort" a list based on the length of the text string?

For example {=sort(["bird", "lizard", "mouse"]) fn(a,b)} ⇒ ["lizard", "mouse", "bird"] or ["bird" "mouse", "lizard"].

I understand how to sort numbers {=sort([1, 5, 9, 3], (a, b) -> (b - a))} but wasn't sure how to apply it to a set of text strings, or if it's possible.

Thanks!

Hi @Josh_Whitchurch I was looking at your question and intrigued me JAJAJA

So I was looking into it and you can combine functions inside Text Blaze. Since the function requires numbers to compare, you can use the len() function to return the length of a string and then compare which word is the longer.

Here the solution with the example you provided.

{note}
Here you can store any list of words that you want to sort.
{A= ["bird", "lizard", "mouse","cat","Dragonfly","cow"]}
{endnote}

Here is the sorted list:

{=sort(A, (a, b) -> len(b) - len(a)) }

Hope it helps

1 Like

Thanks for the great solution @Diego_Suancha. You're becoming a real pro at Text Blaze. I'm starting to feel threatened now...

1 Like

Is just that Text Blaze is such an amazing tool, it have done my job way more easy.

But no worries @Cedric_Debono_Blaze, I think that I am still far from you

1 Like

Thanks for the great reply Diego! Just one note, you never need to embed formulas within formulas, so instead of doing something like `{=...{=len(b)}...}`, you can just do `{=...len(b)...}`