FileDocCategorySizeDatePackage
Sorter.javaAPI DocExample4853Mon Jul 23 13:26:56 BST 2007org.apache.struts2.util

Sorter

public class Sorter extends Object
Sorters. Utility sorters for use with the "sort" tag.
see
org.apache.struts2.views.jsp.iterator.SortIteratorTag
see
SortIteratorFilter

Fields Summary
Constructors Summary
Methods Summary
public java.util.ComparatorgetAscending()

        return new Comparator() {
            public int compare(Object o1, Object o2) {
                if (o1 instanceof Comparable) {
                    return ((Comparable) o1).compareTo(o2);
                } else {
                    String s1 = o1.toString();
                    String s2 = o2.toString();

                    return s1.compareTo(s2);
                }
            }
        };
    
public java.util.ComparatorgetAscending(java.lang.String anExpression)

        return new Comparator() {
            private ValueStack stack = ValueStackFactory.getFactory().createValueStack();

            public int compare(Object o1, Object o2) {
                // Get value for first object
                stack.push(o1);

                Object v1 = stack.findValue(anExpression);
                stack.pop();

                // Get value for second object
                stack.push(o2);

                Object v2 = stack.findValue(anExpression);
                stack.pop();

                // Ensure non-null
                if (v1 == null) {
                    v1 = "";
                }

                if (v2 == null) {
                    v2 = "";
                }

                // Compare them
                if (v1 instanceof Comparable && v1.getClass().equals(v2.getClass())) {
                    return ((Comparable) v1).compareTo(v2);
                } else {
                    String s1 = v1.toString();
                    String s2 = v2.toString();

                    return s1.compareTo(s2);
                }
            }
        };
    
public java.util.ComparatorgetComparator(java.lang.String anExpression, boolean ascending)

        if (ascending) {
            return getAscending(anExpression);
        } else {
            return getDescending(anExpression);
        }
    
public java.util.ComparatorgetDescending()

        return new Comparator() {
            public int compare(Object o1, Object o2) {
                if (o2 instanceof Comparable) {
                    return ((Comparable) o2).compareTo(o1);
                } else {
                    String s1 = o1.toString();
                    String s2 = o2.toString();

                    return s2.compareTo(s1);
                }
            }
        };
    
public java.util.ComparatorgetDescending(java.lang.String anExpression)

        return new Comparator() {
            private ValueStack stack = ValueStackFactory.getFactory().createValueStack();

            public int compare(Object o1, Object o2) {
                // Get value for first object
                stack.push(o1);

                Object v1 = stack.findValue(anExpression);
                stack.pop();

                // Get value for second object
                stack.push(o2);

                Object v2 = stack.findValue(anExpression);
                stack.pop();

                // Ensure non-null
                if (v1 == null) {
                    v1 = "";
                }

                if (v2 == null) {
                    v2 = "";
                }

                // Compare them
                if (v2 instanceof Comparable && v1.getClass().equals(v2.getClass())) {
                    return ((Comparable) v2).compareTo(v1);
                } else {
                    String s1 = v1.toString();
                    String s2 = v2.toString();

                    return s2.compareTo(s1);
                }
            }
        };