Last active
December 18, 2015 05:59
-
-
Save jpountz/5736766 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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