-1

This is the class of my object (I need timestamp is a string):

public class Evento{
    public String timestamp;

    public Evento(String timestamp) {
        super();
        this.timestamp = timestamp;
    }
    public String getTimestamp() {
        return timestamp;
    }
}

In my activity I have:

Evento evento = new Evento[10];
for(int i = 0; i<10; i++)
    evento[i] = new Evento(/*Casual String timestamp*/);

Then I have populated the array, how can I sort it by the timestamp inserted?

2
  • 6
    Arrays.sort() and a custom implementation of the Comparator<T> interface Commented Mar 28, 2015 at 16:23
  • In java 8, assuming that you want a "natural" sort on the string value, you can create the comparator like: Comparator<Evento> comparator = Comparator.comparing(Evento::getTimestamp); docs.oracle.com/javase/8/docs/api/java/util/… Commented Mar 28, 2015 at 16:29

1 Answer 1

0

As previously stated, you are going to need a custom implementation of the Comparator interace. Since it is a string, I would first convert the timestamp to a date object: Converting string timestamp to date , and then you could follow this example to sort by date Sort objects in ArrayList by date?

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

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.