Let’s say I have an object with one significant criteria, and perhaps some other data.
class MyObject {
int criteria;
String otherData;
}
I want to “shuffle-sort” a list such that given the data x,y such that x is the criteria and y is the otherData, all the similar x’s are grouped (and sorted) but within the subgroup, it is shuffled
My goal given sequential runs, might give the following results
/ 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
My current plan is to create a Comparable that only compares the first criteria. Then my “shuffle-sort” could simply
list.shuffle(); // get a random ordering
list.sort(); // now group by criteria, leaving the others in a still random state
My question is, is this the most efficient way to do this? Is it going to actually achieve my objective? Is there some pattern that might emerge from this? If so, what?
I believe this will work and is asymptotically optimal. It’s easiest to analyze if you make sure to use a stable sort, but I think the grouping phase won’t introduce bias regardless.
If you want to write code that more obviously expresses your intent, you could insert values into a
TreeMap<Integer, List<MyObject>>, grouping all values of a given integer into the same list. Then iterate over the contents of the map in key-order (lowest to highest), shuffling each sublist and then dumping its contents into a final output list. In my eyes that approach is more “obviously correct” but I believe yours works just as well.