I'm new to programming and I'm having some difficulties sorting an ArrayList of objects with a date and time attribute into chronological order. I have run many tests on my isBefore method, which determines which Tweet object is before the other, and have determined it to be fine. This means that my issue is probably in the algorithm I use to sort my ArrayList.
private void sortTwitter(){
ArrayList<Tweet> temp = new ArrayList<Tweet>();
while(getSizeTwitter()>1){
int indexOfEarliest = 0;
for(int i = 0; i<getSizeTwitter(); i++){ //finds the index of the earliest tweet in the tweets ArrayList
//The Tweet object at [0] is tweets.get(0).
//The Tweet object we want to compare it to initially is tweets.get(1). Specifically, we want tweets.get(1).getDate() and .getTime()
Tweet tweet1 = this.tweets.get(indexOfEarliest);
Tweet tweet2 = this.tweets.get(i);
boolean tweet1Earlier = tweet1.isBefore(tweet2.getDate(), tweet2.getTime());
if(tweet1Earlier == false){ //Supposed to update the value of indexOfEarliest to the index of tweet2 is tweet2 is earlier.
indexOfEarliest = i;
}
}
temp.add(this.tweets.get(indexOfEarliest));
this.tweets.remove(indexOfEarliest);
}
//Now tweets is an array of 1 element, the largest.
temp.add(this.tweets.get(0));
this.tweets.clear();
this.tweets = temp;
}
If I run the full program without the sorting method, I get this as output:
If I use the "sorting" algorithm, I get this:
If anyone could please explain to me what I am doing wrong or point me in the right direction, I would be very grateful. Thanks!
Update: isBefore
public boolean isBefore(String sTestDate, String sTestTime){
String[] splitTweetDate = this.date.split("-"); //Reconfigures the format of Tweet instance date and time
String[] splitTweetTime = this.time.split(":");
int[] tweetDate = stringToInt(splitTweetDate);
int[] tweetTime = stringToInt(splitTweetTime);
String[] splitTestDate = sTestDate.split("-"); //Reconfigures the format of query parameters
String[] splitTestTime = sTestTime.split(":");
int[] testDate = stringToInt(splitTestDate);
int[] testTime = stringToInt(splitTestTime);
for(int i = 0; i<3; i++){ //Tests the date year first, then month, then day. Returns true if tweetDate is earlier than testDate.
if(tweetDate[i]<testDate[i]){
return true;
}
}
for(int i = 0; i<3; i++){ //If on the same day, tests hour first, then minutes, then seconds. Returns true if tweetTime is earlier than testDate.
if(tweetTime[i]<testTime[i]){
return true;
}
}
return false; //If method returns nothing, message was posted at the same time or later.
}
//HELPER 2
private int[] stringToInt(String[] arrString){ //converts an array of strings into an array of integers.
int[] arrInt = new int[arrString.length];
for(int i = 0; i<arrInt.length; i++){ //parseing the elements of the string array to integers.
arrInt[i] = Integer.parseInt(arrString[i]);
}
return arrInt;
}
Someone has suggested that I use compares and I will if no-other solution is presented to me but I don't think I am technically allowed to (this is for an assignment).