title: java function definition with object creation
body: <p>In the below code, I want to understand what does the Collections.sort function is doing. What does this means </p>
  
  <pre><code>Collections.sort(list, new Comparator() { a complete java function });
  </code></pre>
  
  <p>I understand that <code>new Comparator()</code> means a class object, but what is the purpose of the function block here, and what we call it in java.</p>
  
  <p>Thanks.</p>
  
  <pre><code>/**
       * Sorts/shuffles the given list according to the current sending queue 
       * mode. The list can contain either Message or Tuple&lt;Message, Connection&gt; 
       * objects. Other objects cause error. 
       * @param list The list to sort or shuffle
       * @return The sorted/shuffled list
       */
      @SuppressWarnings(value = "unchecked") /* ugly way to make this generic */
      protected List sortByQueueMode(List list) {
          switch (sendQueueMode) {
  
      case Q_MODE_RANDOM:
          Collections.shuffle(list, new Random(SimClock.getIntTime()));
          break;
      case Q_MODE_FIFO:
          Collections.sort(list, 
                  new Comparator() {
              /** Compares two tuples by their messages' receiving time */
              public int compare(Object o1, Object o2) {
                  double diff;
                  Message m1, m2;
  
                  if (o1 instanceof Tuple) {
                      m1 = ((Tuple&lt;Message, Connection&gt;)o1).getKey();
                      m2 = ((Tuple&lt;Message, Connection&gt;)o2).getKey();
                  }
                  else if (o1 instanceof Message) {
                      m1 = (Message)o1;
                      m2 = (Message)o2;
                  }
                  else {
                      throw new SimError("Invalid type of objects in " + 
                              "the list");
                  }
  
                  diff = m1.getReceiveTime() - m2.getReceiveTime();
                  if (diff == 0) {
                      return 0;
                  }
                  return (diff &lt; 0 ? -1 : 1);
              }
          });
          break;
      /* add more queue modes here */
      default:
          throw new SimError("Unknown queue mode " + sendQueueMode);
      }
  
      return list;
  }
  </code></pre>
  
  <p>I finally got he understanding of comparator. This simple example would help:</p>
  
  <pre><code> Collections.sort(ls, new Comparator() 
                             {
  
                              public int compare(Object o1, Object o2) 
                              {
                              String sa = (String)o1;
                              String sb = (String)o2;
  
                              int v = sa.compareTo(sb);
  
                              return v;           
  
                                  // it can also return 0, and 1
                              }
                             }    
                      );
  </code></pre>