0

Im calling collections.sort on an array list of objects of class appointment but it wont sort nothing happens

This is how im calling the collections sort

 ArrayList<appointment> listoffappointments = new ArrayList<appointment>();

 //then everytime on button click user adds a new appointment after setting the following code is executed 
{
 Calendar calendar1 = Calendar.getInstance();                
 calendar1.set(mYear, mMonth, mDay, temphour, tempmin);
 appointment tempppt = new appointment(c2,tempstring1);
 listoffappointments.add(tempppt);
 Collections.sort(listoffappointments);
}

Here is the class can someone find the issue thanks

import java.util.Calendar;

public class appointment  implements Comparable<appointment> {

Calendar time;
String Text;
public appointment(Calendar tempc, String temptext) {
    Text = temptext;
    time = tempc;
}

public void settext(String text)
{
    Text = text;
}
public String gettext()
{
    return Text;
}
public String toString() {
    return Text;
}
public Long gettimeofappt()
{
    return time.getTimeInMillis();

}

@Override
public int compareTo(appointment o) {
     return (this.gettimeofappt() < o.gettimeofappt() ) ? -1: (this.gettimeofappt() > o.gettimeofappt() ) ? 1:0 ;  

}


}
7
  • 1
    where is your collections.sort call? Commented Apr 4, 2014 at 8:33
  • in my other class that has the array list of objects of class appointment Commented Apr 4, 2014 at 8:40
  • You should include that code in your question. Commented Apr 4, 2014 at 8:41
  • added it in the question now @nKn Commented Apr 4, 2014 at 8:42
  • assume i already have elements of type appointment in my arraylist Commented Apr 4, 2014 at 8:43

2 Answers 2

2

you are returning a Long object, Change your compareTo like:

@Override
public int compareTo(appointment o) {
     return this.gettimeofappt().compareTo(o.gettimeofappt() );

}

Long.compareTo compares two Long objects numerically, and Retunrs:

the value 0 if this Long is equal to the argument Long; a value less than 0 if this Long is numerically less than the argument Long; and a value greater than 0 if this Long is numerically greater than the argument Long (signed comparison).

Sign up to request clarification or add additional context in comments.

3 Comments

even with "return this.gettimeofappt().compareTo(o.gettimeofappt() );" nothing changes @blackbelt
can you print the content of the ArrayList ? Just for debugging puropse
contents of array list come out exactly in order they wen in so first then first second then first second third
1

Try this:

Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return s1.compareToIgnoreCase(s2);
        }
    });

Look at Sorting ArrayList (String ArrayList and custom class)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.