To answer your question: Create another column that's true/false based on whether or not code1 = code2, and use that to sort.
However, I strongly recommend you use this as an excuse to learn Power-Query instead! I think you'll love it! In Excel, Power-Query starts with the 'Get & Transform' section of the Data ribbon.
- Create a third spreadsheet.
- Stumble through using the "Data - Get & Transform" buttons in the ribbon to create a query for one of your two original spreadsheets. For now just close the Power-Query window.
- Repeat for your second original spreadsheet.
- Back in Excel, if you click "Data - Get & Transform - Show Queries" from the ribbon, you should see a sidebar of your two queries.
- Right click on your first query and click "Reference". This will create a new query that builds upon your old one and opens the Power-Query Editor window.
- Within the Power-Query Editor window, click "Merge". This will bring up a dialog box. Select your second query and highlight the primary key (code1 and code2) on each of the tables. Then take a look at the "Join-Kind" field and choose whatever works for you before clicking ok.
- When you merge, you're just creating a new column that contains the results of your merge. To expand them into data columns, you need to click the little icon on the right of that column's title. This will bring up a dialog box you can fill in.
Here's the awesome thing about Power-Query: You can stumble through it very easily without fear because you are not changing your original data! Just try it, see how it looks, change it... or scrap it and remove the step... or the entire query and start all over! If you change any queries or your source data changes, you can just do "Data - Refresh All" from the ribbon to completely recalculate your query results. Plus, you can do some amazing things like unpivot data! It's definitely worth learning.