I have created an ArrayList<MyObjects>
. MyObject
has an attribute priority
, and implements the comparable method, which I have overriden.
I want to:
Sort ArrayList<MyObjects>
based on the priority
attribute. I've done that with:
Collections.sort(ArrayList<MyObjects>)
BUT, after the sorting, I want to randomly shuffle (only) the elements that have the same priority
, keeping the general order of the priority.
ArrayList <myObject> objectList = new ArrayList <MyObject>();
objectList = {object1, object2, object3, object4,
object5, object6, object7, object8, object9}
Collections.sort(objectList)
myObject has a compareTo()
method that compares the priority
of two myObject.
Supose that, after the sorting, the order of the objects is:
object7
object8
object4
object5
object1 // priority = 2
object2 // priority = 2
object3 // priority = 2
object9
object8
object1
, object2
, and object3
all have the same priority.
How could I shuffle them without affecting the other objects?
That is, have a list:
object7
object8
object4
object5
objectx // priority = 2
objecty // priority = 2
objectz // priority = 2
// where {x, y, z} are randomly selected from {1, 2, 3}
object9
object8
I thought of doing a random assignment in the compareTo()
method when it has an equality.
@Override
public int compareTo(MyObject otherObject) {
int comparison;
if (this.priority < otherObject.priority) {comparison = +1;}
else if (this.priority > otherObject.priority) {comparison = -1;}
else {
Random generator = new Random();
comparison = generator.nextInt( 3 ) - 1;
}
return comparison;
But I suppose that when I call the Collections.sort(ArrayList<MyObjects>)
the randomness will not hold.
Should I create my own sorting algorithm?
Or a shuffling algorithm?
Or does there already exists one algorithm that shuffles a specific set of elements in a list?
Shuffle the list before you sort it. Collections.sort
is guaranteed to be a stable sort, so equal elements will be in the same order that they were in the unsorted collection... you just need to make sure that that order is a suitably random one. Collections.shuffle
is the simplest way of doing this:
Collections.shuffle(objectList);
Collections.sort(objectList);
See more on this question at Stackoverflow