Ok,
as the subject of my first post states, This is from Lab 1016(5.2) Sort a 
list using Comparator. In that lesson, there is a class named 
MyCompartors.class, that class contains a method called stringComparator()
.
1) The first line of that method is a return statement and there is another 
one at the bottom of the same stringComparator() method. Why does it have 
multiple return statements?
2) The Main.java file calls on the MyComparators.stringComparator(); method, 
but never uses the compare(Object o1, Object o2) method which i s the only 
method there. Please provide explanation on this.

Code should be available on the Collections labs, but I am attaching them 
here anyway.

On Monday, January 6, 2014 2:15:36 PM UTC-5, Deepak A L wrote:
>
> hi.
>     how does it work...confused.
>
> attach the working example team of jpassion with correct explanation....
>
> Regards,
> Deepak
> On Jan 6, 2014 12:14 AM, "Sergio G Barreros" 
> <[email protected]<javascript:>> 
> wrote:
>
 

-- 
You received this message because you are subscribed to the Google Groups 
"JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
Visit this group at http://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/groups/opt_out.
package sortingcomparator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;


public class Main {
    
    public static void main(String[] args) {
        
        // Create an ArrayList object and add items to it.
        ArrayList a1 = new ArrayList();
        a1.add("Boston");
        a1.add("New York");
        a1.add("Seoul");
        a1.add("Tokyo");
        a1.add("London");
        a1.add("Bangkok");
        System.out.println("Before sorting = " + a1);
        
        // Get String Comparator object and sort the list
        Comparator comp = MyComparators.stringComparator();
        Collections.sort(a1, comp);
        
        // Display the sorted list
        System.out.println("Sorted list using String Comparator = " + a1);
        
        // Create an ArrayList object and add items to it.
        ArrayList a2 = new ArrayList();
        a2.add(new Integer(33));
        a2.add(new Integer(17));
        a2.add(new Integer(45));
        a2.add(new Integer(100));
        a2.add(new Integer(3));
        System.out.println("Before sorting = " + a2);
        
        // Get Integer Comparator object and sort the list
        Comparator comp2 = MyComparators.integerComparator();
        Collections.sort(a2, comp2);
        
        // Display the sorted list
        System.out.println("Sorted list using Integer Comparator = " + a2);

        // Create an ArrayList object and add items to it.
        ArrayList a3 = new ArrayList();
        a3.add(new Date(45785678));
        a3.add(new Date(12345678));
        a3.add(new Date(23456789));
        System.out.println("Before sorting = " + a3);

        // Get Integer Comparator object and sort the list
        Comparator comp3 = MyComparators.dateComparator();
        Collections.sort(a3, comp3);

        // Display the sorted list
        System.out.println("Sorted list using Integer Comparator = " + a3);
    }
}


package sortingcomparator;

import java.util.Comparator;
import java.util.Date;

public class MyComparators {
    
    // String Comparator object
    public static Comparator stringComparator() {
        
        return new Comparator() {
            
            public int compare(Object o1, Object o2) {
                String s1 = (String)o1;
                String s2 = (String)o2;
                int len1 = s1.length();
                int len2 = s2.length();
                int n = Math.min(len1, len2);
                char v1[] = s1.toCharArray();
                char v2[] = s2.toCharArray();
                int pos = 0;
                
                while (n-- != 0) {
                    char c1 = v1[pos];
                    char c2 = v2[pos];
                    if (c1 != c2) {
                        return c1 - c2;
                    }
                    pos++;
                }
                return len1 - len2;
            }
        };
    }
    
    // Integer Comparator object
    public static Comparator integerComparator() {
        return new Comparator() {
            
            public int compare(Object o1, Object o2) {
                int val1 = ((Integer)o1).intValue();
                int val2 = ((Integer)o2).intValue();
                return (val1<val2 ? -1 : (val1==val2 ? 0 : 1));
            }
        };
    }
    
    // Date  Comparator object
    public static Comparator dateComparator() {
        return new Comparator() {
            
            public int compare(Object o1, Object o2) {
                long val1 = ((Date)o1).getTime();
                long val2 = ((Date)o2).getTime();
                return (val1<val2 ? -1 : (val1==val2 ? 0 : 1));
            }
        };
    }
}

Reply via email to