title: How to use comparator with arraylist
body: <p>I have an exercise to output all the strings that user inputs in ascending order. <br>
  The program should stop when user enters "<code>quit</code>".</p>
  
  <pre><code>   import java.util.Scanner;
     import java.util.ArrayList;
     import java.util.Comparator;
     import java.util.Collections;
     public class Exercise2{
          public static void main (String[] args) {
          ArrayList&lt;String&gt; list=new ArrayList&lt;&gt;();
          Scanner scanner = new Scanner(System.in);
          String word="string";
          while(!word.equals("quit")){            // cycle will continue till string will contain a word "quit".
               System.out.println("Please enter a string line. String can be whatever you want. After you enter a string please press Enter");
          word=scanner.nextLine();
          if(word.equals("quit"))
          break;
          list.add(word);
          }
          Collections.sort(list, new Comparator&lt;String&gt;(){
          public int compare(String o1, String o2){
                  if(o1.length()&gt;o2.length()){
                       return 1;
              }else{
                      return o1.compareTo(o2);
                  }
      }
  
  });
  
      System.out.println(list);
  }
  }
  </code></pre>
  
  <p>Could anyone explain why my code is not working?<br> I would like to do it with Comparator, please.</p>