Sorting arraylists by Date object
in
Programming Questions
•
1 year ago
I am in the process of creating a program that uses the Twitter API (through the twitter4j library) to search for tweets based on certain criteria and visualize them by date/time. I am imagining that I would break the tweets up into specific intervals (by hour, day, etc) and the number of tweets that was sent during that time would create the magnitude of the shape.
So that is the main idea, but I am having trouble sorting and categorizing the data by the date. From twitter, I am creating an arraylist of tweet objects that have a few parameters, one of which is a Date parameter which comes in as "Fri Mar 30 14:27:17 CDT 2012".
My initial thought was to split this Date into separate peices and recombine it into a string or int that I could then sort with a comparator. Using a series of split commands I got the date information into an INT format of "YYmmDDhhMM" that i then sorted with a comparator.
I thought I could then sort this into a series of arraylists based on it's size, but realized (I may be wrong) that I would have to create hundreds of arraylists manually to recreate all the days and hours over the time period of the tweets, especially since a period of time with no tweets is just as significant to the data as a period of time with many tweets.
here is a simple example of what I mean. (not code, more of a diagram of the data)
FYI, I am and architecture student and not particularly experienced with processing or any programming languages but I am learning!
Let me know if you need more information or explanation.
Thanks!
So that is the main idea, but I am having trouble sorting and categorizing the data by the date. From twitter, I am creating an arraylist of tweet objects that have a few parameters, one of which is a Date parameter which comes in as "Fri Mar 30 14:27:17 CDT 2012".
My initial thought was to split this Date into separate peices and recombine it into a string or int that I could then sort with a comparator. Using a series of split commands I got the date information into an INT format of "YYmmDDhhMM" that i then sorted with a comparator.
- void setup() {
//Credentials
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("###");
cb.setOAuthConsumerSecret("###");
cb.setOAuthAccessToken("##");
cb.setOAuthAccessTokenSecret("###");
//Make the twitter object and prepare the query
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("insert query here");
query.setRpp(100);
ArrayList tweetdata = new ArrayList();
//Try making the query request.
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Tweet t = (Tweet) tweets.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
String date = d.toString(); -
- //------------how I split the date to convert to an int-------------------
- String date_split[] = date.split(" ");
//split and make numbers of the date
if (date_split[1].equals("Jan") == true) {
date_split[1] = "01";
}
else if (date_split[1].equals("Feb") == true) {
date_split[1] = "02";
}
else if (date_split[1].equals("Mar") == true) {
date_split[1] = "03";
}
else if (date_split[1].equals("Apr") == true) {
date_split[1] = "04";
}
else if (date_split[1].equals("May") == true) {
date_split[1] = "05";
}
else if (date_split[1].equals("Jun") == true) {
date_split[1] = "06";
}
else if (date_split[1].equals("Jul") == true) {
date_split[1] = "07";
}
else if (date_split[1].equals("Aug") == true) {
date_split[1] = "08";
}
else if (date_split[1].equals("Sep") == true) {
date_split[1] = "09";
}
else if (date_split[1].equals("Oct") == true) {
date_split[1] = "10";
}
else if (date_split[1].equals("Nov") == true) {
date_split[1] = "11";
}
else if (date_split[1].equals("Dec") == true) {
date_split[1] = "12";
}
println(date_split[1]);
String month = date_split[1];
String day = date_split[2];
String year = date_split[5];
String year_split[] = year.split("");
String year_half[] = {
year_split[3], year_split[4]
};
String year_joined = join(year_half, "");
String time = date_split[3];
String time_split[] = time.split(":");
String hours = time_split[0];
String mins = time_split[1];
String sec = time_split[2];
String joinedDates[] = {
year_joined, month, day, hours, mins
};
String jDates = join(joinedDates, "");
int intDates = parseInt(jDates);
- // now dates are int in "YYmmDDhhMM" format
tweetObj data = new tweetObj(user, msg, intDates);
tweetdata.add(data);
println(data.date);
}
Collections.sort(tweetdata, new AComparator());
println("count" + ":" + tweets.size());
for (int f =tweetdata.size()-1;f>=0; f--) {
tweetObj test = (tweetObj) tweetdata.get(f);
println( test.date);
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
} - class AComparator implements Comparator {
int compare(Object o1, Object o2) {
int score1 = ((tweetObj) o1).returnDate();
int score2= ((tweetObj) o2).returnDate();
return (score1<score2) ? -1 : (score1==score2) ? 0 : 1;
}
} - class tweetObj {
String user;
String msg;
String loc;
String src;
int hours;
int mins;
int sec;
int date;
tweetObj(String u, String m, int d) {
user = u;
msg = m;
date = d;
}
int returnDate(){
return date;
} - }
I thought I could then sort this into a series of arraylists based on it's size, but realized (I may be wrong) that I would have to create hundreds of arraylists manually to recreate all the days and hours over the time period of the tweets, especially since a period of time with no tweets is just as significant to the data as a period of time with many tweets.
here is a simple example of what I mean. (not code, more of a diagram of the data)
- date/time number of tweets
- apr. 17th, btw 9 and 10 am 6
- apr. 17th, btw 10 and 11 am 3
- apr. 17th, btw 11 and 12 am 0
- apr. 17th, btw 12 and 1 pm 5
- apr. 17th, btw 1 and 2 pm 12
FYI, I am and architecture student and not particularly experienced with processing or any programming languages but I am learning!
Let me know if you need more information or explanation.
Thanks!
1