This is a test program for sorting an Array of Objects, based on 1 of the objects fields. I was able to overload Java's CompareTo method and call Arrays.sort(Object) -- This works well if the sort field is fixed. But I want an example where I can press "r", "g" or "a" on the keyboard and it will sort dynamically based on correct field (response, gender, age).
It is compiling and running, but I'm getting a strange bug where the first one or two Key Pressed work okay, then I have to hit the key TWICE for it to sort. And there a bug where hitting, say, 'r' and 'g' intermittently causes problems... I am stumped. Any ideas?
PFont font;
Person[] people;
Person[] tmp;
char sortVal;
void setup()
{
size(800, 400);
font = loadFont("HelveticaNeue-Light-18.vlw");
textFont(font);
textAlign(CENTER, CENTER);
noStroke();
smooth();
//import the XML data
XMLElement xml = new XMLElement(this, "people.xml");
//split all the people up into an array of XMLElements
XMLElement[] kids = xml.getChildren("row");
//Make a new, blank array that will hold 10 People
people = new Person[10];
//Go through the array of people, fill that index with a new Person, based on the
//information in this index of the XMLElement array.
for (int i=0; i<people.length; i++)
{
//create temp vars to store the answer, gender, age data and
//pass them into the Constructor for this Person.
int res = int(kids[i].getChild("answer").getContent());
int gend = int(kids[i].getChild("gender").getContent());
int age = int(kids[i].getChild("age").getContent());
people[i] = new Person(age, res, gend);
println(people[i].sorter);
}
tmp = people;
}
void draw()
{
background(255, 250, 240);
for (int i=0; i<people.length; i++)
{
tmp[i].update();
tmp[i].display(50+(i*70), height/2);
}
}
void keyPressed()
{
if (key=='r')
{
sortVal='r'; //reminder: sortVal is global -- not part of the Person class...
}
else if (key=='a')
{
sortVal='a';
}
else if (key=='g')
{
sortVal='g';
}
else
{
println("Not a valid sort option");
}
println(sortVal);
tmp = people; //replace the reference to the original People array
Arrays.sort(tmp); //sort the reference
// the idea here was to always sort from the original ordering...
I've been exploring the Twitter4j library for a class demo, with Jer Thorp's tutorial as a starting point.
So far everything has worked well, but I notice that every time I try to grab a geo location, it's consistently NULL.
Am I missing something? Or can it really be that SO MANY tweets have no geo-locations associated with them?? I've tried every method, object/constructor and field I can find with "geo" in the name... Everything has been null.
For testing, this code just pulls one tweet using noLoop() and a getTweet() function. A mouse event gets a new tweet...
//Build an ArrayList to hold all of the words that we get from the imported tweets
ArrayList<String> words = new ArrayList();
ConfigurationBuilder cb = new ConfigurationBuilder();
Twitter twitter;
void setup() {
//Set the size of the stage, and the background to black.
size(100, 100);
background(0);
smooth();
//Credentials
cb.setOAuthConsumerKey("yours");
cb.setOAuthConsumerSecret("
yours
");
cb.setOAuthAccessToken("
yours
");
cb.setOAuthAccessTokenSecret("
yours
");
//Make the twitter object and prepare the query
twitter = new TwitterFactory(cb.build()).getInstance();