2

I'm wondering how Excel would sort identical values. I have ID numbers, but there are instances where there would be a duplicate ID number. I'm unsure about how Excel would decide how to order these? Would it just look at the information in the adjacent row and then compare those values as part or the ordering?

If I had:

Column A | Column B
166          C
166          A
166          B

And then sorted on Column A in descending order, would I get:

Column A | Column B
166          C
166          A  
166          B

Or would I get:

Column A | Column B
166          A
166          B
166          C

Thanks.

5
  • 1
    You can add extra columns as tie breakers if you want. But in the absence of that, the order of rows with equal values is essentially random. The sort is unstable. Commented Nov 9, 2015 at 21:58
  • While I don't know the answer, I assume it would just read columns left to right. But you'd be wise to be explicit and specify a 2nd column if you care about it. Commented Nov 9, 2015 at 21:59
  • 1
    @pnuts - even more of a bother than typing out this well thought out question! :) Commented Nov 9, 2015 at 22:01
  • 2
    I don't think Excel uses information in adjacent columns unless you specifically tell it to by adding those to the "sort" columns defined via the UI dialog. Commented Nov 9, 2015 at 22:12
  • 1
    spreadsheet and database sorts are usually stable. Adjacent cells not specified for the sort are ignored when comparing rows. Commented Nov 9, 2015 at 23:01

1 Answer 1

3

Excel's sort is supposed to be a stable sort, so the order of equal elements should be preserved. You should be able to do something similar to radix sort, sorting rows doing multiple sorts, from least significant column to most significant column. You can sort rows by 3 columns at a time.

If using Visual Basic to do the sort, note that selection sort and quick sort are not stable. Use insertion sort (stable) or bubble sort (stable) instead. I don't know if it's possible to implement merge sort (stable) with VBA.

A non-stable sort can be made stable by including the row number in a compare as the least significant comparator, if this is possible with VBA.

Sign up to request clarification or add additional context in comments.

10 Comments

...so the order of equal elements should be preserved: sounds reasonable (and is supported by a quick test.) Do you have an authoritative reference to support this claim?
@chrisneilsen - microsoft example for how to sort rows by more than 3 columns. Spreadsheet and data base sorts are usually stable sorts.
Excel ALWAYS performs stable sorts.
VBA QuickSort is not stable
And lots of other sorts aren't either. But sorting from the UI is.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.