//sortVec5.java -- by tsaiwn@csie.nctu.edu.tw
//demo to sort Vector with Comparator by using Collections.sort()
import java.util.*;
import java.io.*;
//void print(Vector<int>);
//bool comppp(int a, int b) { return a > b; }  // a compare function
//The following class is a comparator
class God implements Comparator<Integer> {   // note
   public int compare(Integer aa, Integer bb) {
       int a = aa.intValue( ); 
       int b = bb.intValue( ); 
       return a - b;   // Ascending order
   }
};
class sortVec5 {
  // main program starts here
  static Integer x[ ] = { 38, 49, 25, 58, 85, 66 };
  public static void main(String [ ] xxx ) {
   God arben = new God( );   // Note !
   Vector<Integer> v = new Vector<Integer> (Arrays.asList(x));
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   v.add(0, 33);  // into head
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   v.add(v.size(), 77);
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   v.add(0, 99);  // head again
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
   printf("Original Vector: ");
   print(v);
   //Arrays.sort(v);   // Error, can only apply to array
   Collections.sort(v);
   printf("After sort: ");
   print(v);
   //sort(v.begin( ), v.end( ), comppp);    // java can NOT pass function
   //printf("After sort with comppp: ");
   //print(v);
   v.add(1, 68);
   printf("After v.add(1,68): "); print(v);
   Collections.sort(v, arben);   // with Comparator
   printf("After sort with arben: ");
   print(v);
   printf("capacity=%d, size=%d\n", v.capacity(), v.size());
  }//main(
  static void print(Vector<Integer> x){
   Iterator i;
   for(i= x.iterator( ); i.hasNext( ); )
      printf("%s ", ""+i.next());
   printf("\n");
  }//print(
  static void printf(String f, Object... o){
     System.out.printf(f, o);
  }
}// class
/*****************
D:\COURSE\OOP\ppnt>javac sortVec5.java
D:\COURSE\OOP\ppnt>java sortVec5
capacity=6, size=6
capacity=12, size=7
capacity=12, size=8
capacity=12, size=9
Original Vector: 99 33 38 49 25 58 85 66 77
After sort: 25 33 38 49 58 66 77 85 99
After v.add(1,68): 25 68 33 38 49 58 66 77 85 99
After sort with arben: 25 33 38 49 58 66 68 77 85 99
capacity=12, size=10

D:\COURSE\OOP\ppnt>
*********************/
