Sort data by one or more columns, keeping rows together

Updated

Sorting puts the rows of a worksheet in order by the values of one or more columns: smallest to largest diameter, or by line and then by diameter within each line. In a statistics worksheet each row belongs to one part or one sample, so a correct sort moves every column of the row together. Sorting is a way to look at data, find extremes and group rows; it is not a step most analyses need.

When to use it

  • You want to see the largest and smallest values with the rows they came from: which part, which lot, which operator.
  • You want rows grouped by a category, such as line or supplier, before reading them or copying a block to a report.
  • You need a list in a fixed order, for example parts in part-number order for a measurement study.
  • You want to check for duplicates or typing errors, which often show up next to each other once sorted.

Do not sort data you will chart in time order, such as data for a run chart or a control chart, unless you can sort it back.

How to do it in StatLab

  1. Select a cell in the column you want to sort by. The dialog starts from it.
  2. Choose Data › Sort….
  3. Under Columns to sort, choose the columns that move. All columns are chosen at first, which keeps every row together. Leave that as it is unless you have a reason.
  4. Choose Sort by and its Order (Ascending or Descending). To break ties, add up to two Then by columns, each with its own order.
  5. Press OK. Results records the sort, and Ctrl+Z undoes it.

How values are ordered:

  • Numbers and dates in numeric order.
  • Text the way people read it: upper and lower case sort together, and numbers inside text compare by value, so Line 2 comes before Line 10.
  • Missing values go last, whether the order is ascending or descending.
  • Ties keep their order. Rows with equal keys stay in the order they had, so sorting is predictable and repeatable.

Example

A turning cell runs three lines. An inspector logs eight parts with their part number and diameter (mm). Part 103 could not be measured, so its diameter is missing.

Before:

C1-T LineC2 PartC3 Diameter
Line 1010112.031
Line 210212.018
Line 10103*
Line 110412.044
Line 210512.052
Line 1010612.027
Line 110712.009
Line 210812.035

She wants each line together, with its largest diameter first. In Data › Sort… she keeps all three columns under Columns to sort, chooses Sort by Line, Ascending, and Then by Diameter, Descending. Results: Sorted 3 columns by C1-T Line ↑, C3 Diameter ↓.

After:

C1-T LineC2 PartC3 Diameter
Line 110412.044
Line 110712.009
Line 210512.052
Line 210812.035
Line 210212.018
Line 1010112.031
Line 1010612.027
Line 10103*

Three details to notice. Line 10 comes after Line 2, because the number inside the text is compared by value. Within Line 10, the missing diameter is last even though the order is descending. And every diameter is still next to its own part number.

How to read the results

  • Read the top and bottom rows. After sorting by a measurement, the extremes are at the ends, with their part, lot or operator beside them. Here the largest diameter, 12.052 mm, is part 105 on Line 2.
  • Count the missing values at the end. They are collected at the bottom of each group, which makes it easy to see how many parts in each group were not measured.
  • Check the Results line. It names the columns that moved and the sort keys with their direction (↑ ascending, ↓ descending).

Common mistakes

  • Sorting one column on its own. If only Diameter is chosen under Columns to sort, the diameters are put in order but the part numbers stay where they were. In the example that would put 12.009 next to part 101, which is wrong. Keep every column chosen unless you really want to reorder one column alone, and undo at once if you did it by accident.
  • Destroying the time order. Control charts, run charts and many tests assume the rows are in production order. Before sorting, add a sequence column (Calc › Make patterned data… from 1 to the number of rows) so you can sort back by it.
  • Text that looks like numbers. If a numeric column became text because of one stray value, it sorts as text. Fix the column with Change data type first.
  • Sorting to remove outliers. Sorting makes extreme values easy to see, and tempting to delete. Find out why a value is extreme before you remove it: it may be the most important part in the data.

Frequently asked questions

How do I sort from largest to smallest?

Choose Descending under Order for that key. Each key has its own order, so you can sort by Line ascending and Diameter descending at the same time.

Where do missing values go?

Always last, in ascending and descending order alike.

Why does Line 10 come after Line 2?

Numbers inside text compare by value, the way people read them. Plain character-by-character sorting would put Line 10 before Line 2.

Can I undo a sort?

Yes. A sort is one step: press Ctrl+Z. To get back the original order after other changes, sort by a sequence column you added beforehand.

Are upper and lower case sorted separately?

No. line a and Line A sort together.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab