java - Sorted Arraylist printing out in wrong order -


i have arraylist of temperatures , days. , sorted arraylist using insertion sort method. having when go print out not print out in sorted manner prints out:76, 84, 81, 88, 87. because commented out collections.sort(dailytemps) line? erroring out commented out

here code:

    import java.io.*; import java.util.scanner; import java.util.arrays;  public class dailytemperature      {      //variables     private double temperature;     private string day;      //gettemp & settemp methods     public double gettemp()     {       return temperature;     }      public void settemp(double newtemp)     {       temperature = newtemp;     }      //getday & setday methods     public string getday()     {       return day;     }      public void setday(string newday)     {       day = newday;     }      //constructor   public dailytemperature(string day, double temperature)   {     this.day = day;     this.temperature = temperature;   }    //compareto method used comparable interface   public int compareto(dailytemperature other)   {     if (temperature < other.temperature) return -1;     if (temperature == other.temperature) return 0;     return -1;   }     //tostring method print out original arraylist contents , sorted arraylist contentes   public string tostring()   {     return("day of week: " + this.getday() +            " - temperature: " + this.gettemp());   } }    import java.io.*; import java.util.scanner; import java.util.arraylist;  public class dailytemperaturelist {   public static void main (string [] args)   {     arraylist<dailytemperature> dailytemps = new arraylist<dailytemperature>();      dailytemps.add(new dailytemperature("mon", 87.1));     dailytemps.add(new dailytemperature("tue", 88.3));     dailytemps.add(new dailytemperature("wed", 81.2));     dailytemps.add(new dailytemperature("thu", 84.0));     dailytemps.add(new dailytemperature("fri", 76.3));      //original list printout     system.out.println("original list:" );     system.out.println( dailytemps.tostring() );     system.out.println(" "); //empty line      //method sort array list     insertionsort(dailytemps);     //sorted list printout     system.out.println("sorted list:" );     system.out.println( dailytemps.tostring() );      //collections.sort(dailytemps);   }    static void insertionsort(arraylist<dailytemperature> dailytemps)   {     dailytemperature temp = null;     int position = 0;      //loop 2nd element on     (int = 1; < dailytemps.size(); i++)     {       temp = dailytemps.get(i);       position = i;        while ( 0 < position && temp.compareto(dailytemps.get(position - 1)) < 0)       {         dailytemps.set(position, dailytemps.get(position - 1));         position--;       }       dailytemps.set(position,temp);     }   } } 

your compareto() implementation incorrect. never return 1 if this.temperature greater other.temparature.

  public int compareto(dailytemperature other)   {     if (temperature < other.temperature) return -1;     if (temperature == other.temperature) return 0;     return -1; // return 1; here   } 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -