您的位置:首页 > 其它

CareerCup How would you implement a secondary sorting

2014-03-15 00:24 323 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: