-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?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
aletede91
  • 1,137
  • 2
  • 16
  • 30
  • 6
    `Arrays.sort()` and a custom implementation of the `Comparator` interface – EpicPandaForce Mar 28 '15 at 16:23
  • In java 8, assuming that you want a "natural" sort on the string value, you can create the comparator like: `Comparator comparator = Comparator.comparing(Evento::getTimestamp);` https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#comparing-java.util.function.Function- – Brett Okken Mar 28 '15 at 16:29

1 Answers1

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?

Community
  • 1
  • 1
Derek_M
  • 1,018
  • 10
  • 22