title: Sorting objects in an arraylist by price attributes
body: <p>Im working on this really small project, I've got an arraylist of car objects and im trying to print all the cars out in price ascending order. When i look around online every seems to be doing it with an array rather than are arraylist?</p>
  
  <p>Any ideas on how i can make this possible with an array list? Below is the code i have.</p>
  
  <pre><code>public ArrayList&lt;Car&gt; sortPriceAcending(ArrayList&lt;Car&gt; cars) {
  
      ArrayList&lt;Car&gt; sortedCars = new ArrayList&lt;Car&gt;();
      Car temp;
  
      for (int j = 0; j &lt; cars.size() - 1; j++) {
  
          for (int i = 0; i &lt; cars.size() - 1; i++) {
              Car car = (Car) cars.get(i);
              Car car1 = (Car) cars.get(i + 1);
              if (car.getPrice() &gt; car1.getPrice()) {
                  temp = car;
                  cars.get(i) = cars.get(i + 1);
                  cars.get(i + 1) = temp;
                  sortedCars = cars;
  
              }
          }
      }
      return sortedCars;
  }
  </code></pre>