//sortVec2.java  ---  @copyLeft by tsaiwn@csie.nctu.edu.tw
// sorting  example using  comparator
//can ONLY handle Vector of Integer in this example
import java.util.*;
public class sortVec2 {   java.io.PrintStream cout = System.out;
   public static void main(String xxx[ ]) { new sortVec2( ); }
   sortVec2( ) { main( ); }
   //bool compare (int i,int j) { cout << " ha "; return (i<j); }
   //the following defines a Comparator class "Sorter"
   class Sorter implements Comparator<Integer> {  
         // note the following function in this class
      //bool operator() (int i,int j) { cout << " S "; return (i<j);}
      public int compare(Integer i, Integer j) {  cout.print( " S " );  
         return i - j;   // auto unbox; Ascending
      }
   } //Sorter
   Sorter myObject = new Sorter( );

   //template function to print Vector of any type 
   <T>void printV(Vector<T> aVector) {
     //Vector<int>::iterator it;
       Iterator<T> it = aVector.iterator( );  // Java
       cout.print("\n Vector contains:");    // print out content:
       for ( ; it.hasNext( ); ) cout.print(" "+  it.next( ) );
       cout.println( );
   }// printV(
int main ( ) {
  //int myints[ ] = {32,71,12,45,26,80,53,33};  // C/C++
  Integer myints[ ] = {32,71,12,45,26,80,53,33};  // Java
   //vector<int> v= vector(myints, myints+sizeof myints/sizeof myints[0]);
  Vector<Integer> myVector;
  myVector = new Vector<Integer>(Arrays.asList(myints));
  cout.print( "Vector contains:" );
  printV(myVector);

  // using default compareTo( ) function; Java has to sort ALL
  //sort (myVector.begin(), myVector.begin()+4);   // first 4 elements
  Collections.sort(myVector);
  cout.print("After sort:"); printV(myVector);

  Collections.sort(myVector, Collections.reverseOrder( ));
  cout.print("After reverse sort:"); printV(myVector);

  // using function as comp     // 12 32 45 71(26 33 53 80)  part
  //sort (myVector.begin()+4, myVector.end(), compare);
  // Java can NOT pass function 
  // using object as comp     // all (12 26 32 33 45 53 71 80)
  Collections.sort (myVector, myObject);
  cout.print("\nSort ALL with Comparator myObject:"); printV(myVector);

  Collections.sort(myVector, Collections.reverseOrder(myObject));
  cout.print("\n with reverseComparator of myObject:"); printV(myVector);
  return 0;
}//main(
}// class sortVec2
/**********       *****   ª`·N running Results, why there is the "S"?
D:\COURSE\OOP\ppnt>javac sortVec2.java

D:\COURSE\OOP\ppnt>java sortVec2
Vector contains:
 Vector contains: 32 71 12 45 26 80 53 33
After sort:
 Vector contains: 12 26 32 33 45 53 71 80
After reverse sort:
 Vector contains: 80 71 53 45 33 32 26 12
 S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S
Sort ALL with Comparator myObject:
 Vector contains: 12 26 32 33 45 53 71 80
 S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S  S
 with reverseComparator of myObject:
 Vector contains: 80 71 53 45 33 32 26 12
*****************************************************************/
