Skip to content

Instantly share code, notes, and snippets.

@jpountz
Last active December 18, 2015 05:59
Show Gist options
  • Select an option

  • Save jpountz/5736766 to your computer and use it in GitHub Desktop.

Select an option

Save jpountz/5736766 to your computer and use it in GitHub Desktop.
// Both arrays must have the same length
// scores[i] is the score of objects[i]
final Object[] objects = ...;
final float[] scores = ...;
new IntroSorter() {
float pivotScore;
@Override
protected void swap(int i, int j) {
// Swap values at offsets i and j in both arrays
swap(objects, i, j);
swap(scores, i, j);
}
@Override
protected int compare(int i, int j) {
// times -1 because we want to have highest scores first
return - Float.compare(scores[i], scores[j]);
}
@Override
protected void setPivot(int i) {
// save the score for further comparisons
pivotScore = scores[i];
}
@Override
protected int comparePivot(int j) {
// times -1 because we want to have highest scores first
return - Float.compare(pivotScore, scores[j]);
}
}.sort(0, objects.length); // this is when the sort happens
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment