Java List Compare compare(List> beanList1, List> beanList2, final int[] sortKeys, final boolean[] sortAscending, final boolean nullAlwaysFirst, final int naOrder)

Here you can find the source of compare(List> beanList1, List> beanList2, final int[] sortKeys, final boolean[] sortAscending, final boolean nullAlwaysFirst, final int naOrder)

Description

compare

License

Open Source License

Declaration

@SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T extends Object> int compare(List<Comparable<?>> beanList1, List<Comparable<?>> beanList2,
            final int[] sortKeys, final boolean[] sortAscending, final boolean nullAlwaysFirst, final int naOrder) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    public final static int SMALLER = -1;
    public final static int GREATER = 1;
    public final static int EQUAL = 0;
    public final static String NA = "N/A";
    public final static int NA_NONE = 0, NA_FIRST = 1, NA_LAST = 2;

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static <T extends Object> int compare(List<Comparable<?>> beanList1, List<Comparable<?>> beanList2,
            final int[] sortKeys, final boolean[] sortAscending, final boolean nullAlwaysFirst, final int naOrder) {
        int comp = EQUAL;
        for (int i = 0; i < sortKeys.length; i++) {
            if (sortKeys[i] < 0) {
                continue;
            }/*from  ww w. j  a v a  2s . co m*/
            boolean asc = true;
            try {
                asc = sortAscending[i];
            } catch (Exception e) {
            }

            if (sortKeys[i] >= beanList1.size())
                return 0;
            Comparable<?> field1 = beanList1.get(sortKeys[i]);
            Comparable<?> field2 = beanList2.get(sortKeys[i]);

            //TODO .... 
            if (naOrder != NA_NONE) {
                if (isNA(field1) && !isNA(field2)) {
                    comp = naOrder == NA_FIRST ? SMALLER : GREATER;
                    return comp;
                } else if (!isNA(field1) && isNA(field2)) {
                    comp = naOrder == NA_FIRST ? GREATER : SMALLER;
                    return comp;
                }
            }

            if (field1 == null) {
                if (field2 != null) {
                    comp = SMALLER;
                    if (nullAlwaysFirst) {
                        return comp;
                    }
                }
            } else if (field2 == null) {
                comp = GREATER;
                if (nullAlwaysFirst) {
                    return comp;
                }
            } else if (!field1.equals(field2)) {
                try {
                    if (asc) {
                        return ((Comparable) field1).compareTo(field2);
                    } else {
                        return ((Comparable) field2).compareTo(field1);
                    }
                } catch (Exception e) {
                    return EQUAL;
                }
            }

            if (comp != EQUAL) {
                if (asc) {
                    return ((comp == SMALLER) ? SMALLER : GREATER);
                } else {
                    return ((comp == SMALLER) ? GREATER : SMALLER);
                }
            }
        }
        return EQUAL;
    }

    private static boolean isNA(Object na) {
        return na == null || NA.equalsIgnoreCase(na.toString());
    }

    private static boolean isNA(Comparable<?> na) {
        return na == null;
    }
}

Related

  1. canonicalCompare(List o1, List o2, Comparator elemComparator)
  2. compare(List lhs, Collection rhs, List standard)
  3. compare(List list1, List list2)
  4. compare(List a, List b)
  5. compare(List strList1, List strList2)
  6. compare(List a, List b)