CareerCup How would you implement a secondary sorting

来源:互联网 发布:英语培训机构知乎 编辑:程序博客网 时间:2024/05/06 15:08

how would you implement a secondary sorting. Meaning sorting by Category A, and then sub sorting by category B?

--------------------------------------------------------------------------


Three approaches come to mind. 
1) Sort on A, then scan the list for runs of elements with the same value for A, then sort each sublist for field B. 
2) Sort on A/B simultaneously by using a compare function or key function that uses both fields. 
3) Sort the list on B, then stable-sort the list on A. 

A nice feature of approach #1 is that you can do it lazily, i.e. only sort the sublists when the second field is queried.



0 0
原创粉丝点击