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 :grinning_face_with_smiling_eyes:

1 Like

Cool thread @Josh_Whitchurch.

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

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 :grimacing:

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)...}

I tweaked your example above to follow that practice.

1 Like

Thank you @Diego_Suancha! This is great. Exactly what I was looking for. @scott Thanks for the added clarity on the formula as well.

@Cedric_Debono_Blaze Don't feel too threatened haha. I've referenced your posts on multiple occasions, which are always helpful. Keep it up!

2 Likes

@Josh_Whitchurch, glad to be of help :grin: