Sorting Arrays with CompareTo
              in 
             Programming Questions 
              •  
              1 year ago    
            
 
           
             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...
 - }
 - //The Person Class
 - class Person implements Comparable<Person>
 - {
 - int age;
 - int response;
 - int gender;
 - int sorter;
 - int original;
 - Person(int _age, int _response, int _gender)
 - {
 - age = _age;
 - response = _response;
 - gender = _gender;
 - original = 0;
 - }
 - void display(float x, float y)
 - {
 - if (gender==0)
 - fill(80, 100, 230);
 - else
 - fill(230, 80, 170);
 - ellipse(x, y, age, age);
 - char temp;
 - if (response==0) temp='N';
 - else temp='Y';
 - fill(255);
 - text(temp, x, y);
 - fill(100);
 - text(age, x, height/2-50);
 - }
 - void update()
 - {
 - if (sortVal=='a') sorter=age;
 - else if (sortVal=='g') sorter=gender;
 - else if (sortVal=='r') sorter=response;
 - else sorter=original;
 - }
 - /* Overload compareTo method */
 - public int compareTo(Person tmp)
 - {
 - if (this.sorter < tmp.sorter)
 - {
 - /* instance lt received */
 - return -1;
 - }
 - else if (this.sorter > tmp.sorter)
 - {
 - /* instance gt received */
 - return 1;
 - }
 - /* instance == received */
 - return 0;
 - }
 - }
 
             The data is being read from this XML file:
            
            
              <rows>
             
             
                <row>
             
             
                  <age>23</age>
             
             
                  <answer>1</answer>
             
             
                  <gender>1</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>24</age>
             
             
                  <answer>1</answer>
             
             
                  <gender>0</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>15</age>
             
             
                  <answer>0</answer>
             
             
                  <gender>1</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>32</age>
             
             
                  <answer>1</answer>
             
             
                  <gender>1</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>30</age>
             
             
                  <answer>1</answer>
             
             
                  <gender>0</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>29</age>
             
             
                  <answer>0</answer>
             
             
                  <gender>0</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>29</age>
             
             
                  <answer>0</answer>
             
             
                  <gender>0</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>28</age>
             
             
                  <answer>0</answer>
             
             
                  <gender>1</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>45</age>
             
             
                  <answer>0</answer>
             
             
                  <gender>1</gender>
             
             
                </row>
             
             
                <row>
             
             
                  <age>62</age>
             
             
                  <answer>1</answer>
             
             
                  <gender>1</gender>
             
             
                </row>
             
             
              </rows>
             
             
              
              1  
            
 
            