-2

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:

Without "sorting"

If I use the "sorting" algorithm, I get this:

With "sorting"

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).

  • Possible duplicate of [Sort Java Collection](https://stackoverflow.com/questions/6957631/sort-java-collection) – Kaustubh Khare Dec 06 '17 at 06:20
  • Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and [edit] your question accordingly. See also: [How to Debug Small Programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Joe C Dec 06 '17 at 06:21
  • Use Collections.sort(yourList,new TweetComparator()). Go though this documentation on how to implement compartor https://www.javatpoint.com/Comparator-interface-in-collection-framework – Santosh Dec 06 '17 at 06:21
  • maybe you should also show the `isBefore` method, the logic of posted code seems correct (bit hard to understand). Actually a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) would be helpfull (much more than screenshots of the result) – user85421 Dec 06 '17 at 07:06

1 Answers1

0

Create separate sorting mechanism by using java.util.Comparator and provide criteria by comparing java.util.Date and simply call Collection.sort(<customList>, Comparator<>); thats it, no need to loop manually. please check examples how to implement Comparator using java.util.Date, you will get an idea. it will reduce your number of lines of code as well.

Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
  • I realized I hadn't specified that in the context of the assignment, I'm not allowed to used Comparator as we haven't learned it in class. Thank you for the very valid solution, but it's not one I'm allowed to implement. – Chloe Davidson Dec 06 '17 at 15:54