Results 1 to 3 of 3
  1. #1
    Brandon Seale is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Sorting Array High-Low

    I have an array called list that contains string id, and double temp //names and values for weather stations

    I want to sort the list by the value temperature.


    public void Display2()
    {
    Arrays.sort(List);-
    HOW DO I SORT BY TEMP?? GetID is the getter method for string id, and GetTemp is the getter method for double Temp. Temp and ID were initialized on another .java file --WeahterStationData.java if that helps
    System.out.println("Highest Temperature: " + List[0].GetID() + List[0].GetTemp());
    System.out.println("Lowest Temperature: " + List[CurrSize].GetID() + List[CurrSize].GetTemp());

    }

    ERRORS

    Exception in thread "main" java.lang.ClassCastException: WeatherStationData cannot be cast to java.lang.Comparable
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.mergeSort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at WeatherStationList.Display2(WeatherStationList.jav a:58)
    at WeatherStationUI.RunIT(WeatherStationUI.java:36)
    at WeatherStationUI.<init>(WeatherStationUI.java:18)
    at Main.main(Main.java:11)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default

    If you read my reply in your other thread on the very same topic you would have seen my advice that your class needs to implement Comparable or you can write a Comparator.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way: Have WeatherStationData implement Comparable<WeatherStationData> and then give it an appropriate compareTo(WeatherStationData otherWSData) method.

    Edit: I have just read Junky's post. Why the heck are you making us waste time re-answering questions that have already been answered? Please stick to one thread on the subject -- the previous thread. Let's lock this one.

Similar Threads

  1. Sorting Array UI
    By Brandon Seale in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 01:50 AM
  2. Help with sorting an object array.
    By TommyLR in forum New To Java
    Replies: 1
    Last Post: 02-03-2011, 11:43 AM
  3. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:42 AM
  4. Sorting an Array via Stacks
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 02-01-2010, 06:53 AM
  5. Storing high score and sorting the array
    By Implode in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 12:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •