Komma

Sort a CSV on Mac in Numbers while keeping headers and rows together

Open a CSV in Numbers, mark its field-name row as a header, sort the entire table by your chosen column, then export a CSV copy. Keep each record’s values together so that product IDs remain attached to the correct quantities.

By Komma · Reviewed: · Documentation and synthetic samples checked; no native app test

Keep a copy and a way back to the original order

Duplicate the CSV and open the copy in Numbers. Confirm that product IDs, quantities and notes are in the correct columns before sorting. If you will need the original order later, add a working column named original_order with fixed values 1, 2, 3 and so on for the data rows.

Avoid copying just the quantity column elsewhere, sorting it and pasting it back. That changes quantities independently of product IDs. The unit being moved should be the entire record.

If the first row contains field names such as id and quantity, select the table, open Format → Table and set the header row count to one. This converts the existing first row into a header; it does not insert a blank row. Do not apply it to a CSV that starts with data instead of field names.

Bold text alone is not evidence that a row is a header. Check the setting described in Apple’s header row documentation.

Sort the entire table in Numbers

  1. Select the table and open Sort in the Organize sidebar.
  2. Choose Sort Entire Table.
  3. Use Add a Column to select quantity, then choose Ascending.
  4. For a defined order among equal quantities, add id ascending as the second rule. Click Sort Now if needed.

Headers stay outside the sort while each product row moves together. These steps follow Apple’s table sorting documentation. Labels may vary between versions.

Check a small example with quantities 1, 2, 2 and 10

The unsorted sample CSV contains a header and four products. Sorting quantity numerically ascending, then id ascending for ties, should give this order:

Before: P003=10, P001=2, P002=2, P004=1
After:  P004=1, P001=2, P002=2, P003=10
original_order: 4, 2, 3, 1

Download the expected CSV to compare. It is a synthetic result generated and checked across all 25 cells with Python, not an export captured from Numbers. P001’s multiline note and P003’s comma-containing note should travel with their products.

An order of 1, 10, 2, 2 suggests a text comparison. Treat quantities as numbers, but do not convert product codes just to sort quantities. Check IDs with leading zeros separately. Decide how blanks, quantities with unit labels and unknown values should be handled before sorting real data.

If headers move or the order looks wrong

  • If the header moves into the data, undo and check the header row setting.
  • If only part of the table moves, check for selected-row sorting or sorting within categories.
  • If sorting is unavailable, check for merged cells. Unmerge them in a copy if the original layout matters.
  • Hidden rows also participate in sorting; visible rows alone do not establish the complete result.

Immediately after a mistake, use Edit → Undo. Later, sort the numeric original_order values ascending to restore the initial order. Sorting quantity descending will not reconstruct it.

Export a separate CSV and compare the records

Use File → Export To → CSV with a different filename. Check the table and encoding in the export settings. If the destination requires a fixed set of columns, remove the working original_order column from the delivery copy while retaining your original copy.

Reopen the exported CSV. Check its header, product count, first and last IDs, and order among equal quantities. Each ID must still have its original product name, quantity and note. For CSV with multiline fields, counting physical text lines is not enough to count records.

What was checked

We reviewed Apple’s documentation on October 27, 2026. We sorted the downloadable examples in Python using numeric quantity and ID keys, then checked cell associations, write/read results and restoration of the original order. We did not perform a native Numbers test for this article.

Back to CSV editing guides