Excel VBA How does Excel sort duplicate values?

天涯浪子 提交于 2021-02-05 07:42:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!