Automatically Sort Data in Alphabetical Order using Formula

Excel built-in data sorting is amazing, but it isn’t dynamic. If you sort data and then add data to it, you would need to sort it again.

Sort Data in Alphabetical Order

In this post, I will show you various ways to sort data in alphabetical order using formulas. This means you can add data, and it will automatically sort it for you.

When the Data is all Text with No Duplicates

Suppose you have a data as shown below:Sort data in alphabetical order - text dataset

In this example, all the data is in text format (no numbers, blanks or duplicates). To sort this, I will use a helper column. In the column next to the data, use the following COUNTIF formula:

=COUNTIF($A$2:$A$9,"<="&A2)

sort data in alphabetical order - Helper column with countif

This formula compares a text value with all the other text values and returns its relative rank. For example, in cell B2, it returns 8, as there are 8 text values that are lower than or equal to the text ‘US’ (alphabetical order).

Now to sort the values, use the following combination of INDEX, MATCH and ROWS functions:

=INDEX($A$2:$A$9,MATCH(ROWS($B$2:B2),$B$2:$B$9,0))

sort data in alphabetical order - Formula Index to get sorted dataThis formula simply extracts the names in the alphabetical order. In the first cell (C2), it looks for the country name that has the lowest number (Australia has 1). In the second cell, it returns Canada (which has the number 2) and so on..

Allergic to Helper Columns??

Here is a formula that will do the same without the helper column.

=INDEX($A$2:$A$9,MATCH(ROWS($A$2:A2),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9),0))

This is an array formula, so use Control + Shift + Enter instead of Enter.

I will leave it for you to de-code.

Try it Yourself.. Download Example FileDownload File Pic

This formula works well if you have text or alphanumeric values.

But it fails miserably if:

  • You have duplicates in the data (try putting US twice).
  • There are blanks in the data.
  • You have a mix of numbers and text (try putting 123 in one of the cells).
When Data is a Mix of Numbers, Text, Duplicates, & Blanks

Now this one is a bit tricky. I will use 4 helper columns to show you how it works (and then give you a huge formula that will do it without the helper columns). Suppose you have a data as shown below:sort data in alphabetical order - blank duplicate 1

You can see there are duplicate values, blank and numbers. So I will use helper columns to address each of these issues.

Helper Column 1

Enter the following COUNTIF formula in Helper Column 1

=COUNTIF($A$2:$A$9,"<="&A2)

sort data in alphabetical order - blank duplicate 2

This formula does the following:

  • It returns 0 for blanks.
  • In the case of duplicates, it returns the same number.
  • Text and numbers are processed parallelly and this formula returns the same number for text and number (for example 123 and India both get 1).

Helper Column 2 

Enter the following IS function in Helper Column 2:

=--ISNUMBER(A2)

sort data in alphabetical order - blank duplicate 3

Helper Column 3

Enter the following formula in Helper Column 3:

=--ISBLANK(A2)

sort data in alphabetical order - blank duplicate 4

Helper Column 4

Enter the following formula in Helper Column 4

=IF(ISNUMBER(A2),B2,IF(ISBLANK(A2),B2,B2+$C$10))+$D$10

sort data in alphabetical order - blank duplicate 5

The idea for this formula is to segregate blanks, numbers and text values.

  • If the cell is blank, it returns the value in cell B2 (which would always be 0) and adds the value in cell D10. In a nutshell, it will return the total number of blank cells in the data
  • If the cell is a numerical value, it will return the comparative rank and add the total number of blanks. For example, for 123 it returns 2 (1 is the rank of 123 in the data, and there is 1 blank cell)
  • If it is text, it returns the comparative rank and add the total number of numerical values and blanks. For example, for India, it add the text’s comparative rank in text (which is 1) and adds the number of blank cells and the number of numerical values.

Final Result – Sorted Data

Now we will use these helper columns to get the sorted list. Here is the formula:

=IFERROR(INDEX($A$2:$A$9,MATCH(SMALL($E$2:$E$9,ROWS($F$2:F2)+$D$10),$E$2:$E$9,0)),"")

sort data in alphabetical order - blank duplicate 6

This method of sorting now becomes fool-proof. I have shown you the method for 8 items, but you can extend it to as many items as you want.

Try it Yourself.. Download Example FileDownload File Pic

One Formula to Sort it All (without Helper Columns)

If you can handle extreme formulas, here is an all-in-one formula that will sort data in alphabetical order (without any helper column).

Here is the formula:

=IFERROR(INDEX($A$2:$A$9,MATCH(SMALL(NOT($A$2:$A$9="")*IF(ISNUMBER($A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9)+SUM(--ISNUMBER($A$2:$A$9))),ROWS($A$2:A2)+SUM(--ISBLANK($A$2:$A$9))),NOT($A$2:$A$9="")*IF(ISNUMBER($A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9),COUNTIF($A$2:$A$9,"<="&$A$2:$A$9)+SUM(--ISNUMBER($A$2:$A$9))),0)),"")

Enter this formula in a cell and drag it down to get the sorted list. Also, since this is an array formula, use Control + Shift + Enter instead of Enter.

This formula has real world utility. What do you think? I would love to learn from you. Leave your footprints in the comments section!

Related Tutorials:
  • Ryan

    Great tip! I don’t understand the big formula in the end, but it seems to do the trick! I am using it going forward. Thanks for sharing

    • Thanks for commenting Ryan! Glad it works for you.

  • Rose

    A bit complicated but the formulas help – Thanks

    • Thanks for commenting Rose. I know the final formula is a bit too much to handle. But I have tried making it robust to handle all situations (numbers, text, duplicates and blanks).

  • Dhiraj

    Nice

    • Thanks for commenting Dhiraj.. Glad you liked it 🙂

  • Pierre Sammut

    Great tip, thanks

    • Thanks for commenting Pierre.. Glad you liked it 🙂

  • Oana Ionita

    Wow, thanks for this amaizing exemple!!! I have had really needed it.

    • Thanks for commenting Oana.. This type of dynamic sorting helps save a lot of time. Glad you find it useful

  • hubert

    very clear and helpful… thank you Sumit!

    • Thanks for commenting Hubert.. Glad you liked it 🙂

  • MF

    Real world utility? I would guess it is good for feeding in a NamedRange for Data Validation, so that you validating list always come in sorted order. What you say?

    • Thanks for commenting. Named range + Data validation would make an amazing use of it. It can also be used to get the sorted data in another location/worksheet (in case of employee names or any types of Ids). I got this query from a friend who wanted to sort id names and numbers using a formula.

  • Raghu BK

    Its very helpful tip , Thanks Sumit

    • Thanks Raghu.. Glad you found it helpful 🙂

  • Sukarno

    Thats a great tip indeed.. it really works for me, although I had to put lot of mind to understand it.

  • Sukarno

    Thanks so much

  • Shoban Babu Medam

    NICE.

    • Glad you liked it 🙂

      • Shoban Babu Medam

        Shortcut for Referencing Ranges
        A shortcut is available when referencing ranges. The shortcut uses square brackets, as shown in Table .
        Shortcuts for Referencing Ranges
        Standard Method ShortcutRange
        (“D5”) [D5]
        Range(“A1:D5”) [A1:D5]
        Range(“A1:D5, G6:I17”) [A1:D5, G6:I17]
        Range(“MyRange”) [MyRange]

  • Ankur

    Hey Sumit bhai you did a awesome job…. i have also made a formula …..

    =IFERROR(INDEX($A$1:$A$15,MATCH(SMALL(IF(ISTEXT($A$1:$A$15),COUNTIF($A$1:$A$15,”<="&$A$1:$A$15)*15^2,IF(ISNUMBER($A$1:$A$15),COUNTIF($A$1:$A$15,"<="&$A$1:$A$15))),ROW(1:1)),IF(ISTEXT($A$1:$A$15),COUNTIF($A$1:$A$15,"<="&$A$1:$A$15)*15^2,IF(ISNUMBER($A$1:$A$15),COUNTIF($A$1:$A$15,"<="&$A$1:$A$15))),0)),"")

  • Prachi Patel

    Hi..I am trying to use this formula but am facing a problem. My datasheet is different as i have a list of names and the scores against it ( which obviously will have duplicates). I have arranged that using helper 1 in a new column. If I use this formula, the same name gets repeated for the same score. Please help with this problem. Am stuck at this since 4 days now. Thanks in advance.

  • James Cline

    I am trying to sort my data that has been pasted into a spread sheet that will transpose to another sheet and place the names in alphabetic order.

    • Hello James, you can use the TRANSPOSE function to do this.

  • Arlen Lushi

    Hi, great formula but I am having some difficulties. The blanks in my data set appear after the last number in the data set therefore when I apply the formula the blanks appear at the top of my sorted list. Is there a method to make these appear below the numbers in the sorted list? Much appareciated!

    • Hello Arlen, I tried replicating what you mentioned here and the blanks were at ṭhe bottom. Have a look at image below

      • Arlen Lushi

        Yes! that’s exactly what I need. Thanks … could you provide a solution please?

        • Use the download file in the tutorial. You’ll get this in the second tab of the workbook.

      • Russ

        This formula is awesome! Can you share how you get the spaces to appear at the bottom?

        • Have a look at the download file (second tab)

  • Niveditha Murugesan

    Hey Sumith . Can u just help me in arranging from descending order? I kinda modified the formula but dint work!

  • Glen

    This works great in the first 30-40 columns, but if I try to use it in column AW for example, it doesn’t work. Any reason for that?

  • Trista Law

    Hello,

    What if I want to sort cells with on Column A which greater than zero on Column C?

    Apple 1
    Orange 1
    Pear 0
    Watermelon 2
    Result:
    Apple 1
    Orange 1
    Watermelon 2

    Is it possible to sort it with a simple formula but not array?
    Many thank!

  • HappyGuy

    Really a great contribution. Helps a lot. Many thanks indeed.

  • jeh

    Hi can someone help me, is there a formula for automatic remove duplicates

  • jeh

    Hi Sumit, can you help me to have a formula for the automatic removal of the duplicate in one colum, appreciate your help. thanks

  • Becky

    Hi- first, you are brilliant- and this formula saved me! But, I have a question…I need the associated data in the other columns to sort WITH my “Client Name” column which I want auto-sorted in Alphabetic order so that I can add Clients without having to manually sort each time. So, for example, I have “Client Originator” and “Client Owner” that is associated with each client. When they auto sort, the Originator and Owner info needs to sort too. Help! Many thx in advance.

    • The better way would be to sort one column only, and based on the sorted data, use VLOOKUP to fetch all the remaining records.

  • Abhishek Gupta

    Sumit even i am facing same problem as prachi. can you share the sheet you have shared with prachi. Prob by prachi was:
    Hi..I am trying to use this formula but am facing a problem. My datasheet is different as i have a list of names and the scores against it ( which obviously will have duplicates). I have arranged that using helper 1 in a new column. If I use this formula, the same name gets repeated for the same score. Please help with this problem. Am stuck at this since 4 days now. Thanks in advance.

    Please help

  • JR Ryan

    Hi Sumit, Thank you for posting such a fantastic tutorial. I needed an array like this to sort data from one source into another, and the “One Formula to Sort it All (without Helper Columns)” solution was perfect once adapted.

    One thing though, could you show me how to add a conditional filter to the match?

    I have a sorting array (thank you ) in “Sheet 2” that works perfectly to grab unsorted data from column “E” in the sheet “Level 4”, and then it sorts the data, drops any blanks and returns a list sorted by “Last Name”. What I need to add to this array formula is the filtering of data based on the “City” in column “G”.

    Here is my amended version of your formula:

    =IFERROR(INDEX(‘Level 4’!$C$2:$G$110,MATCH(SMALL(NOT(‘Level 4’!$E$2:$E$110=””)*IF(ISNUMBER(‘Level 4’!$E$2:$E$110),COUNTIF(‘Level 4’!$E$2:$E$110,”<="&'Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110)+SUM(–ISNUMBER('Level 4'!$E$2:$E$110))),ROWS('Level 4'!$E$2:$E2)+SUM(–ISBLANK('Level 4'!$E$2:$E$110))),NOT('Level 4'!$E$2:$E$110="")*IF(ISNUMBER('Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110)+SUM(–ISNUMBER('Level 4'!$E$2:$E$110))),0),3),"")

    This is what the unsorted data looks like on the "Level 4" sheet:

                C          |          D           |           E           |    F    |   G
    ——————————————————————-
    1|   id number   |  First Name    |   Last Name   |  N/A  |  City

    Where do I start?

    • JR Ryan

      I’ve tried to carry on, but I am thoroughly stuck. I have changed the formula as follows, and although it picks up the first match correctly, it doesn’t parse through the data to give any other matches, just the same one over and over again.

      =IFERROR(INDEX(‘Level 4’!$C$2:$G$110,MATCH(SMALL(–NOT(‘Level 4’!$G$2:$G$110=”Rome”)*NOT(‘Level 4’!$E$2:$E$110=””)*IF(ISNUMBER(‘Level 4’!$E$2:$E$110),COUNTIF(‘Level 4’!$E$2:$E$110,”<="&'Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110)+SUM(–ISNUMBER('Level 4'!$E$2:$E$110))),ROWS('Level 4'!$E$2:$G2)+SUM(–ISBLANK('Level 4'!$E$2:$E$110))),–NOT('Level 4'!$G$2:$G$110=Key!$G$6)*NOT('Level 4'!$E$2:$E$110="")*IF(ISNUMBER('Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110),COUNTIF('Level 4'!$E$2:$E$110,"<="&'Level 4'!$E$2:$E$110)+SUM(–ISNUMBER('Level 4'!$E$2:$E$110))),0),3),"")

      Help! I'm spiraling into excel hell!

      • JR Ryan

        Sumit, please help. You wrote this formula. If anyone can solve this, it’s you!