问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/33618992/excel-vba-how-does-excel-sort-duplicate-values