How to Fix Excel Sort Not Arranging Last Names Alphabetically
Question details
The user needs to sort a dataset by Date and then by Last Name, but the spreadsheet fails to alphabetize the last names correctly.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Organizing data using multiple sort levels, specifically sorting by Date first, followed by Last Name and First Name.
- Observed behavior
- Dates are sorted correctly in the first level, but last names and first names in the subsequent levels do not follow the expected alphabetical order.
Before troubleshooting the sorting issue, ensure your dataset has clear column headers and no merged cells within the data range, as these can disrupt the sorting process.
Clean Hidden Spaces and Apply Custom Sort
Hidden spaces or non-printable characters often break alphabetical sorting. Cleaning the text ensures accurate multi-level sorting.
Excel evaluates space characters before letters. If some names have leading spaces, they will not sort properly alongside names without spaces. Using the TRIM and CLEAN functions will normalize the text.
Insert a new column next to your 'Last Name' column. In the first empty cell of the new column, type the formula =TRIM(CLEAN(B2)) (assuming B2 contains the first last name) and press Enter.
Drag the fill handle down to apply the formula to all rows. Select the newly generated names, copy them, right-click the original 'Last Name' column, and select 'Paste as Values' to replace the messy data.
Select your entire data range. Go to the Data tab on the ribbon and click 'Sort' to open the Custom Sort dialog box.
Add Level 1: Sort by 'Date' (Oldest to Newest). Click 'Add Level'. Add Level 2: Sort by 'Last Name' (A to Z). Click OK to apply the sort.

Verify Data Types and Format
Incorrect data types, such as dates stored as text or numbers formatted improperly, can prevent subsequent sort levels from working correctly.
Easily Sort Complex Data with WPS Office
WPS Spreadsheet offers an intuitive custom sort feature, allowing you to seamlessly organize multiple levels of data, clean up text strings, and manage large datasets with perfect Microsoft Excel compatibility.
- 1. Open your workbook: Launch WPS Spreadsheet and open the file containing your data.
- 2. Select Data and Sort: Highlight your data range, navigate to the Data tab, and click the 'Sort' icon to access the Custom Sort dialog.
- 3. Add Sorting Levels: Set the primary condition (e.g., Date), then click 'Add Level' to specify your secondary condition (e.g., Last Name).
- 4. Apply settings: Verify 'My data has headers' is checked, and click OK to perfectly alphabetize your records.

Frequently Asked Questions
Why does Excel sort some names correctly but ignore others?
This usually happens because of hidden leading spaces before the names. Excel evaluates space characters before alphabetical letters, pushing those names to the top or bottom of the list. Using the TRIM function removes these hidden spaces.
How do I sort by last name if the full name is in one column?
You must first separate the names. Use the Text to Columns feature under the Data tab, or use Flash Fill to extract the last names into a new column, then sort by that new column.
Why is the Custom Sort option greyed out?
Custom Sort may be disabled if your worksheet is protected, if multiple sheets are grouped, or if you are trying to sort a shared workbook with restricted permissions. Go to the Review tab and click 'Unprotect Sheet' to restore functionality.




