Excel sort by length of string

I received a question today on how to sort a list of strings which was actually rather simple to accomplish.

The following list is the original sorting:

  • caca
  • cocacola
  • ca
  • cacacacacola
  • c
  • cacacacacacacaac
  • cacaa

The requested list after sorting should look like this:

  • c
  • ca
  • caca
  • cacaa
  • cocacola
  • cacacacacola
  • cacacacacacacaac

What I first did was to create a separate column to contain the length of each cell. There is a function in Excel which easily calculates the length of a string: LEN(). Now I had two columns, the first unsorted string column and the second length (of the first column string) column. As you probably have realised by now it was a simple matter of sorting the second column instead of the first for the strings to sort by length.

If interested in seeing it I have attached the test sheet for you to play around with:

Length String Sorting

Comments

8 responses to “Excel sort by length of string”

  1. Shane avatar
    Shane

    Excellent solution – saved me heaps of time identifying a spurious entry for deletion in a 100,00 item list: thanks!

  2. Cowpatty Bill avatar

    This is good stuff. Let me take it a step further. How can I have it sort based on the width of the data? For example:

    icicle – 6 letters long
    wagon – 5 letters long

    icicle has more letters but wagon is wider because of the font. How can I sort data based on width, not number of letters? Changing the font is not an option.

  3. seo consultant - Anukant avatar
    seo consultant – Anukant

    i love it.. friend.. from last five day i have been working on this.. but i could not find it.. i thank you
    not only thank you….

    thank you thank you thank you thank you thank you thank you

    now i found the solution.

  4. Sutha Sampanthar avatar
    Sutha Sampanthar

    Many+thanks+for+this+as+it+was+just+what+I+needed.

  5. Niki Kole avatar
    Niki Kole

    Just great and simple! Exactly wha I needed! 😉

  6. Hari avatar
    Hari

    Thank you it worked well…

  7. mr-euro avatar

    @JK

    I thought Excel simply used the same English syntax for all formulae, even on the translated versions.

  8. JK avatar
    JK

    Thank you very much, this made things very easy for me! Especially the example file as syntax is different from english in other languages…

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?