【发布时间】:2011-12-21 15:06:18
【问题描述】:
假设我有一个具有一个重要标准的对象,也许还有一些其他数据。
class MyObject {
int criteria;
String otherData;
}
我想“随机排序”一个列表,使得给定数据 x,y 使得 x 是标准,y 是 otherData,所有相似的 x 都被分组(和排序)但在子组内,它是洗牌
我的目标是连续运行,可能会给出以下结果
/ 1s first\|/ 2s next \|/ then 3s \
-----------------------------------
1,a 1,b 1,c 2,d 2,e 2,f 3,g 3,h 3,i // other data is in
1,a 1,c 1,b 2,e 2,d 2,f 3,i 3,h 3,g // a random order
1,c 1,a 1,b 2,d 2,f 2,e 3,h 3,i 3,g // within the subgroup
1,b 1,c 1,c 2,e 2,d 2,f 3,g 3,h 3,i
我目前的计划是创建一个仅比较第一个标准的 Comparable。然后我的“随机排序”可以简单地
list.shuffle(); // get a random ordering
list.sort(); // now group by criteria, leaving the others in a still random state
我的问题是,这是最有效的方法吗?它会真正实现我的目标吗?有没有可能从中出现一些模式?如果有,是什么?
【问题讨论】:
标签: java algorithm sorting collections shuffle