title: Abstract Data Types, sorting objects by specific variable in a list
body: <p>Alright here we go. </p>
  
  <p>I made an ADT in form of a sortedArrayList, which has an add method looking like this:</p>
  
  <pre><code>public boolean addToArray(T i)
      {
  
  
              int insertPlace = 0;
  
                  for(int j=0;j&lt;size;j++)
                  { 
  
                      if(i.compareTo(sortedArray[j])&lt;0)
                      {
                          insertPlace =j;
                          j = size;
                      }
                  }
                  if(size&gt;0)
                  {
  
  
                          for(int w=size-1; w&gt;=insertPlace;w--)
                          {
                                  sortedArray[size]=sortedArray[w];
                          }
                          sortedArray[insertPlace]=i;
                  }
                  else
                  {
                      sortedArray[0]=i;
                  }
                  size++;
  
  
  
          return true;    
  
  
  }
  </code></pre>
  
  <p>Now, this works wonders for my sorted ADT when the input is Strings. However, instead of strings, I want to add objects to the list in shape of Persons, an object containing 4 variables(String Country, String name, int age, int CPR). I want the Person objects to be sorted in the list by age.</p>
  
  <p>Here is the Person class with the compareTo method for sorting.</p>
  
  <pre><code>public class Person implements Comparable&lt;Person&gt;
  {
      int cpr=200193;
      int age=21;
      String name="John Doe";
      String Country="Uzbekistan";
  
  
      public Person() {
          this.cpr=cpr;
          this.age=age;
      }
      public Person(String name, String Country,int cpr,int age) 
      {
          this.cpr=cpr;
          this.age=age;
          this.name=name;
          this.Country=Country;
      }
  
  
      @Override
      public String toString()
      {
          return "Person [Country= " + Country + ", Name:" + name + ", Cpr: "+cpr+ ", age: "+age+"]";
      }
  
      public int compareTo(Person p)
      {
          int before=-1;
          int after=1;
          int middle=0;
  
  
          if(this.age!=p.age)
          {
              if(this.age&gt;p.age) 
              {
                  return before;
              } 
  
              if(this.age&lt;p.age)
              {
                  return after;
              }
          }
          return middle;
      }
  </code></pre>
  
  <p>Now, the problem is that the objects get sorted, since do not appear in the same order I call them to the list. I just can't figure out how they are sorted and how to make the objects sorted by age in the list. </p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>p1.addToArray(new Person());
  p1.addToArray(new Person("Pete","Germany",111111,86));
  p1.addToArray(new Person("John","Denmark",123456,75));
  p1.addToArray(new Person("Michael Jackson", "America",112345,49));
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Item: Person [Country= America, Name:Michael Jackson, Cpr: 112345, age: 49]
  Item: Person [Country= Uzbekistan, Name:John Doe, Cpr: 200193, age: 21]
  Item: Person [Country= Germany, Name:Pete, Cpr: 111111, age: 86]
  Item: Person [Country= Denmark, Name:John, Cpr: 123456, age: 75]
  </code></pre>