Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, create indices that we will sort using the scores:</p> <pre><code>final double[] scores = // acquire scores Integer[] indices = new Integer[scores.length]; Arrays.sort(indexes, new Comparator&lt;Integer&gt;() { @Override public int compare(Integer i, Integer j) { return -Double.compare(scores[i], scores[j]); } }); </code></pre> <p>Note that we use a <code>-</code> to sort in <em>descending</em> order.</p> <p>Now, we have a ordered our scores using <code>indices</code>, and accounted for the possibility of repeated scores. Next, we will collect all the indices for each score. We will do this by building a map from scores to lists of indices for that score.</p> <pre><code>Map&lt;Double, SortedSet&lt;Integer&gt;&gt; map = new HashMap&lt;Double, SortedSet&lt;Integer&gt;&gt;(); for(int i = 0; i &lt; scores.length; i++) { if(!map.containsKey(scores[i])) { map.puts(scores[i], new TreeSet&lt;Integer&gt;()); } map.get(scores[i]).add(indexes[i] + 1); } </code></pre> <p>We use <code>SortedSet</code> so that the indices come out in order and we add <code>1</code> because your ranks start at one instead of zero.</p> <p>Now, if you iterate over the scores, you can look up the ranks from the <code>HashMap</code>.</p> <pre><code>for(int i = 0; i &lt; scores.length; i++) { SortedSet&lt;Integer&gt; set = map.get(scores[i]); System.out.print(scores[i] + ": "); Iterator&lt;Integer&gt; iterator = set.iterator(); System.out.print(iterator.next()); while(iterator.hasNext()) { System.out.print("," + iterator.next()); } System.out.println(); } </code></pre> <p>When I run this on your example <code>[40, 40, 10, 20]</code>, I see the output:</p> <pre><code>40: 1,2 40: 1,2 10: 3 20: 4 </code></pre> <p>and on <code>[40, 40, 40, 10]</code>:</p> <pre><code>40: 1,2,3 40: 1,2,3 40: 1,2,3 10: 4 </code></pre> <p>and on <code>[10, 10, 20, 40]</code>:</p> <pre><code>10: 3,4 10: 3,4 20: 2 40: 1 </code></pre> <p>From here, it should be easy to modify to get your desired rankings.</p> <p>Let's walk through how this works mentally. We'll use the example <code>[40, 40, 10, 20]</code>. First, we build indexes:</p> <pre><code>indexes = [0, 1, 2, 3] </code></pre> <p>Then we sort indexes using the scores;</p> <pre><code>indexes = [0, 1, 3, 2] </code></pre> <p>Then we collect all the ranks for each score (remember, we add one to the indexes from the previous step):</p> <pre><code>map = { 40 : [1, 2], 10 : [4], 20 : [3] } </code></pre> <p>Then, for each score in <code>scores</code>, we can look up all the ranks in <code>map</code> and dump them to the console.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload