<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">On 04/26/14 13:31, Molly M Safigan
      wrote:<br>
    </div>
    <blockquote
cite="mid:C40B2F181831EF44A88CD7352582780313061C88A2@MERCERMAIL.MercerU.local"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      <div>Dr. Pounds,</div>
      <div><br>
      </div>
      I'm trying to sort the ArrayList&lt;State&gt; by temp and in my
      State class I wrote:
      <div><br>
      </div>
      <div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            style="color: #931a68">public</span>
          <span style="color: #931a68">int</span> compare(State a, State
          b){</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span><span
            style="color: #931a68">if</span>(a.getTemp() &lt;
          b.getTemp()){</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span><span
            style="color: #931a68">return</span> -1;</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span>}</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span><span
            style="color: #931a68">else</span>
          <span style="color: #931a68">if</span> (a.getTemp() ==
          b.getTemp()){</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span><span
            style="color: #931a68">return</span> 0;</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span>}</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco;
          color: rgb(147, 26, 104); ">
          <span style="color: #000000"><span class="Apple-tab-span"
              style="white-space:pre"></span></span>else<span
            style="color: #000000">{</span></div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span><span
            style="color: #931a68">return</span> 1;</div>
        <div style="margin: 0px; font-size: 11px; font-family: Monaco; "><span
            class="Apple-tab-span" style="white-space:pre"></span>}</div>
      </div>
      <div><br>
      </div>
      <div>and it seems to compile in the class but I don't know how to
        call it in the main program. </div>
      <div><br>
      </div>
      <div>Maybe I'm totally off…</div>
      <div><br>
      </div>
      <div>Molly</div>
      <div><br>
      </div>
    </blockquote>
    <br>
    It looks like you are trying to use Java "Comparators", but without
    the comparator "machinery".   Comparators are VERY powerful, but
    are, in my opinion, a bit much for this particular problem.  I will
    talk about them last.  There is another way that you could do this
    problem that is just like some of the things we discussed in class. 
    Let's talk about that first...<br>
    <br>
    <br>
    <b>--- Method ONE - The simple selection sort:</b><br>
    <br>
    Since you are sorting a small number (less than 100 items), if your
    State class has a method called getTemp (or something similar) then
    you could implement the following...<br>
    <br>
    Imagine I have an ArrayList&lt;State&gt; stateData defined...<br>
    <br>
    <tt>// I am looping over all of the elements in my arraylist and
      comparing them in these two loops...</tt><tt><br>
    </tt><tt>for (int i=0; i&lt; stateData.size(); i++ ) {</tt><tt><br>
    </tt><tt>         for ( int j = i+1; j&lt; stateData.size(); j++ ) {</tt><tt><br>
    </tt><tt><br>
    </tt><tt>              // Here I actually do the comparison.  If the
      temp of element "j" is greater than element "i", swap them
                </tt><tt><br>
    </tt><tt>              if ( stateData.get(i).getTemp() &gt;
      stateData.get(j).getTemp() ) {</tt><tt><br>
    </tt><tt>                  </tt><tt><br>
    </tt><tt>                  State hold = stateData.get(i);     //
      Here is where I store the temporary "state information" 
                    </tt><tt><br>
    </tt><tt>                  stateData.set(i,stateData.get(j));</tt><tt><br>
    </tt><tt>                  stateData.set(j,hold);</tt><tt><br>
    </tt><tt><br>
    </tt><tt>             }</tt><tt><br>
    </tt><tt>        }</tt><tt><br>
    </tt><tt>  }</tt><br>
    <br>
    <br>
    Put this section of code right before you print everything out and
    you should be done....<br>
    <br>
    <br>
    <br>
    <b>--- Method TWO - Using Comparators</b><br>
    <br>
    At the top of your program include the following....<br>
    <br>
    <tt>import java.util.Collections;</tt><tt><br>
    </tt><tt>import java.util.Comparator;</tt><br>
    <br>
    In your main, preferably after your ArrayList declaration, create
    the comparator for the temperatures.<br>
    <br>
    <tt>    Comparator&lt;State&gt; tempComparator = new
      Comparator&lt;State&gt;(){</tt><tt><br>
    </tt><tt>    public int compare(State o1, State o2){</tt><tt><br>
    </tt><tt>           if ( o1.getTemp() &lt; o2.getTemp() ) {</tt><tt><br>
    </tt><tt>                   return -1;</tt><tt><br>
    </tt><tt>           }</tt><tt><br>
    </tt><tt>           else if ( o1.getTemp() &gt; o2.getTemp() ) {</tt><tt><br>
    </tt><tt>                  return 1;</tt><tt><br>
    </tt><tt>          }</tt><tt><br>
    </tt><tt>          else return 0;</tt><tt><br>
    </tt><tt>          }</tt><tt><br>
    </tt><tt>   };</tt><br>
    <br>
    Note -- this is comparing two fields in the class (like you were
    trying to do in your example)<br>
    <br>
    Now, right before you print out out results, do the following...<br>
    <br>
    <br>
    <tt>Collections.sort(stateData, tempComparator);</tt><br>
    <br>
    and your arraylist will be sorted as with the selection sort from
    above.  We didn't cover this in class, but based on what you sent me
    I thought you might be trying it out.<br>
    <br>
    <br>
    <br>
    <pre class="moz-signature" cols="72">-- 
Andrew J. Pounds, Ph.D.  (<a class="moz-txt-link-abbreviated" href="mailto:pounds_aj@mercer.edu">pounds_aj@mercer.edu</a>)
Professor of Chemistry and Computer Science
Mercer University,  Macon, GA 31207   (478) 301-5627
<a class="moz-txt-link-freetext" href="http://faculty.mercer.edu/pounds_aj">http://faculty.mercer.edu/pounds_aj</a>
</pre>
  </body>
</html>