Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your Comperator approach would work, but is has a few bugs. First of all I would replace the <code>Double</code>s in <code>MyEntry</code> by <code>double</code>.</p> <p>Comparing <code>Double</code> is not the same as comparing <code>double</code> For example:</p> <pre><code>Double a = 1.0; Double b = 1.0; System.out.println(a == b); System.out.println(a.equals(b)); System.out.println(a.doubleValue()== b.doubleValue()); </code></pre> <p>Will return </p> <pre><code>false true true </code></pre> <p>Then in the comparison you cast to <code>int</code>, but this implies flooring that data. <code>(int) (2 - 1.9)</code> will give <code>0</code> Better is to compare using <code>&lt;</code> and return -1 or 1. </p> <pre><code> public static Comparator&lt;MyEntry&gt; ValueComparator = new Comparator&lt;MyEntry&gt;() { public int compare(MyEntry value1, MyEntry value2) { double rfirst = value1.rank; double rsecond = value2.rank; double dfirst = value1.dist; double dsecond = value2.dist; if (rsecond != rfirst) { return rsecond &lt; rfirst?-1:1; } else if(dsecond!=dfirst){ return dsecond &lt; dfirst ?-1:1; } return 0; } } </code></pre> <p>For your second question you require an index. This could be done in two ways. First option is to include the index in <code>MyEntry</code> like this:</p> <pre><code> public class MyEntry implements Comparable&lt;MyEntry&gt; { private double rank; private double dist; private int index; private static int nextIndex = 0; public MyEntry(double rank, double dist) { this.rank = rank; this.dist = dist; this.index = nextIndex++; } </code></pre> <p>This way you will be able to retain the index but it is not so flexible. </p> <p>A more flexible approach could be to have the index in a separate array, and sort that. </p> <pre><code> class IndexedArrayComparator implements Comparator&lt;Integer&gt;{ MyEntry[] array; public IndexedArrayComparator(MyEntry[] entries){ this.array=entries; } public Integer[] createIndexes(){ Integer[] index = new Integer[array.length]; for(int i =0;i&lt;index.length;i++){ index[i]=i; } return index; } public int compare(Integer i0, Integer i1) { double rfirst = array[i0].rank; double rsecond = array[i1].rank; double dfirst = array[i0].dist; double dsecond = array[i1].dist; if (rsecond != rfirst) { return rsecond &gt; rfirst?-1:1; } else if(dsecond!=dfirst){ return dsecond &gt; dfirst ?-1:1; } return 0; } } </code></pre> <p>You can then use it like this:</p> <pre><code> MyEntry[] entries = new MyEntry[5]; entries[0]= new MyEntry(1.1,5); entries[1]= new MyEntry(1.1,4); entries[2]= new MyEntry(2.1,5); entries[3]= new MyEntry(0.1,3); entries[4]= new MyEntry(3.1,1); IndexedArrayComparator comp = new IndexedArrayComparator(entries); Integer[] index = comp.createIndexes(); Arrays.sort(index,comp); for(int i =0;i&lt;index.length;i++){ MyEntry e = entries[index[i]]; System.out.println(String.format("%2d:r= %3.1f, d= %3.1f" ,index[i],e.rank,e.dist)); } </code></pre> <p>Which will give: </p> <pre><code> 3:r= 0.1, d= 3.0 1:r= 1.1, d= 4.0 0:r= 1.1, d= 5.0 2:r= 2.1, d= 5.0 4:r= 3.1, d= 1.0 </code></pre> <p>The second way of sorting while maintaining the index is also described <a href="https://stackoverflow.com/questions/4859261/get-the-indices-of-an-array-after-sorting">here</a>. Credits to Jon Skeet</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.
    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