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.
--------------------------------------------------------------------------
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.
相关文章推荐
- INI文件的读取
- Building the Testing Pipeline
- 兼容IE与firefox火狐的回车事件(js与jquery)
- IOS 中的CoreImage框架(framework)
- Build.VERSION类
- 继承关系的类初始化和实例化的顺序
- 十大最优秀的Web网站性能测试软件工具
- 在VS2012中调用动态链接库
- c语言学习笔记总结
- CSS垂直居中对齐
- SQL Server 2008 权限管理
- InstallShield 操作INI文件
- 计算机广告学目录
- 在Xcode5中修改整个项目名
- 《深入理解Java虚拟机》笔记
- JavaAPI---5.1: 目录结构
- linux下configure命令详细介绍
- 浅谈-----final关键字
- Sharepoint基本概念
- JVM中的Hello World是如何运行的?