title: How to sort data in repository into alphabetical order using .stream()
body: <p>I am trying to create a method that sorts and outputs data I have stored in a repository into alphabetical order using .stream(). Currently I have a method that sorts the data by CityID in numeric order which I will add below. Is there a way to adapt it to sort the same data but by CityName in Alphabetical order?</p>
  
  <p>CityID Method -</p>
  
  <pre><code>private void listCityDataInCityIdOrder() {
      System.out.format("\033[31m%s\033[0m%n", "City Id Order");
      System.out.format("\033[31m%s\033[0m%n", "=============");
      repository.getItems()
              .stream()
              .sorted()
              .map(c -&gt; c.toString())
              .forEach(str -&gt; System.out.print(str));
  }
  </code></pre>
  
  <p>Data Set -</p>
  
  <pre><code>1,"Cartagena","Spain",3
  "2015",0.2,33,26,6,"S"
  "2016",0.0,33,24,8,"SSW"
  "2017",0.0,32,25,6,"E"
  2,"Glasgow","Scotland",3
  "2015",0.0,19,8,3,"SE"
  "2016",0.1,21,11,6,"SE"
  "2017",2.1,19,11,9,"SW"
  </code></pre>
  
  <p>City Model Class -</p>
  
  <pre><code>package model;
  
  import java.util.ArrayList;
  import java.util.Comparator;
  import java.util.List;
  import java.util.Objects;
  
  /**
   *
   * @author mga
   */
  public class City implements Comparable&lt;City&gt;{
      private final int id;
      private String cityName;
      private String country;
      private List&lt;YearData&gt; yearDataCollection;
  
      private static int lastIdAllocated = 0;
  
      static final char EOLN='\n';
      static final String QUOTE="\"";
  
      public City() {
          this.id = ++lastIdAllocated;
          this.cityName = "TBC";
          this.country = "TBC";
          this.yearDataCollection = new ArrayList&lt;&gt;();
      }
  
      public City(String cityName, String country) {
          this.id = ++lastIdAllocated;
          this.cityName = cityName;
          this.country = country;
          this.yearDataCollection = new ArrayList&lt;&gt;();
      }
  
      public City(String cityName, String country, List&lt;YearData&gt; yearDataCollection) {
          this.id = ++lastIdAllocated;
          this.cityName = cityName;
          this.country = country;
          this.yearDataCollection = yearDataCollection;
      }
  
      public City(int id, String cityName, String country, List&lt;YearData&gt; yearDataCollection) {
          this.id = id;
          this.cityName = cityName;
          this.country = country;
          this.yearDataCollection = yearDataCollection;
          if (id &gt; City.lastIdAllocated)
              City.lastIdAllocated = id;
      }
  
  
      /**
       * @return the id
       */
      public int getId() {
          return id;
      }
  
      // Methods required:
  
      public String getCityName() {
          return this.cityName;
      }
  
      public void setCityName(String cityName) {
          this.cityName = cityName;
      }
  
      public String getCountry() {
          return this.country;
      }
  
      public void setCountry(String country) {
          this.country = country;
      }
  
      public List&lt;YearData&gt; getYearDataCollection() {
          return this.yearDataCollection;
      }
  
      public void setYearDataCollection(List&lt;YearData&gt; yearDataCollection) {
          this.yearDataCollection = yearDataCollection;
      }
  
      public void addYearData(YearData yearData) {
          this.yearDataCollection.add(yearData);
      }
  
  
      @Override
      public String toString() {
          return "\nCity Id: " + id + " - City Name: " + cityName +
                  " - Country: " + country + "\nData: " + yearDataCollection + "\n";
      }
  
  
      public String toString(char delimiter) {
          final char EOLN='\n';
          final String QUOTE="\"";
          String str = Integer.toString(this.id) + delimiter +
                  QUOTE + this.cityName + QUOTE + delimiter +
                  QUOTE + this.country + QUOTE + delimiter +
                  Integer.toString(yearDataCollection.size()) + EOLN;
          for (YearData yearData : yearDataCollection) {
              str += yearData.toString();
          }
          return str;
      }
  
      public boolean equals(Object object) {
          if (this == object) return true;
          if (!(object instanceof City)) return false;
          if (!super.equals(object)) return false;
          City city = (City) object;
          return getId() == city.getId() &amp;&amp;
                  java.util.Objects.equals(getCityName(), city.getCityName()) &amp;&amp;
                  java.util.Objects.equals(getCountry(), city.getCountry()) &amp;&amp;
                  java.util.Objects.equals(getYearDataCollection(), city.getYearDataCollection());
      }
  
      public int hashCode() {
          return Objects.hash(super.hashCode(), getId(), getCityName(), getCountry(), getYearDataCollection());
      }
  
      @Override
      public int compareTo(City compareCity) {
  
          int cityId =
                  ((City) compareCity).getId();
  
          //ascending order
          return this.id - cityId;
  
          //descending order
          //return cityId - this.id;
      }
  
  
      public static Comparator&lt;City&gt; CityComparator = new Comparator&lt;City&gt;() {
          @Override
          public int compare(City city1, City city2) {
  
              String cityName1 = city1.getCityName();
              String cityName2 = city2.getCityName();
  
              //ascending order
              //return cityName1.compareTo(cityName2);
  
              //descending order
              return cityName2.compareTo(cityName1);
          }
     };
  
  }
  </code></pre>