title: How to sort objects by specific fromula using compareTo
body: <p>My program should sort the values of persons according to the formula.</p>
  <pre><code>(Height + Weight)*30% +score40%
  </code></pre>
  <p>So the array has the 6 elements we need to sort and output.</p>
  <h3>Issue</h3>
  <p>I struggle with the calculation formula which actually sorts the values of elements properly using <code>compareTo(Student o)</code></p>
  <h3>Code so far</h3>
  <pre><code>class Student implements  Comparable&lt;Student&gt; {
      private String name;
      private float height, weight, score;
  
      // constructor getters and setters omitted for brevity
  
      public void speak() {
          System.out.println(&quot;I am &quot;+name+&quot;, my height &quot;+height+&quot;, my weight &quot;+weight+&quot;, my score &quot;+score);
      }
  
      public int compareTo(Student  o) {
           if(this.getWeight() &gt; o.getWeight()) {
                            return 1;
                          }
                          else if(this.getWeight() &lt; o.getWeight()) {
                            return -1;
                          }
                          return 0;
      }
  
      public String toString() {
          return &quot;Student [name=&quot;+name+ &quot;,height=&quot;+height+&quot;,weight=&quot;+weight+&quot;,score=&quot;+score+&quot;]&quot;;
      }
  }
  </code></pre>
  <p>The test:</p>
  <pre class="lang-java prettyprint-override"><code>import java.util.Arrays;
  
  public class TestCompare {
      public static void main(String[] args) {
          int i;
          Student ps[] = new Student[6];
          ps[0] = new Student(&quot;zhangsan&quot;, 170, 110, 95);
          ps[1] = new Student(&quot;lisi&quot;, 168, 120, 75);
          ps[2] = new Student(&quot;wangwu&quot;, 165, 115, 88);
          ps[3] = new Student(&quot;zhaoliu&quot;, 172, 121, 90);
          ps[4] = new Student(&quot;zhouqi&quot;, 160, 100, 85);
          ps[5] = new Student(&quot;zhengba&quot;, 166, 119, 70);
          System.out.println(&quot;array sort before:&quot;);
          for (i = 0; i &lt; ps.length; i++) {
              ps[i].speak();
          }
          Arrays.sort(ps);  // call sort method
          System.out.println(&quot;\narray sort after:&quot;);
          for (i = 0; i &lt; ps.length; i++) {
              System.out.println(ps[i]);
          }
      }
  }
  </code></pre>