Example usage for java.text Collator compare

List of usage examples for java.text Collator compare

Introduction

In this page you can find the example usage for java.text Collator compare.

Prototype

@Override
public int compare(Object o1, Object o2) 

Source Link

Document

Compares its two arguments for order.

Usage

From source file:de.blizzy.documentr.web.Functions.java

public static List<JspMacroDescriptor> getMacros() {
    List<IMacroDescriptor> descs = Lists.newArrayList(macroFactory.getDescriptors());
    final Locale locale = LocaleContextHolder.getLocale();
    final Collator collator = Collator.getInstance(locale);
    Collections.sort(descs, new Comparator<IMacroDescriptor>() {
        @Override/*from  www  .j  a  v a2  s .co  m*/
        public int compare(IMacroDescriptor d1, IMacroDescriptor d2) {
            String title1 = d1.getTitle(locale);
            String title2 = d2.getTitle(locale);
            return collator.compare(title1, title2);
        }
    });
    Function<IMacroDescriptor, JspMacroDescriptor> function = new Function<IMacroDescriptor, JspMacroDescriptor>() {
        @Override
        public JspMacroDescriptor apply(IMacroDescriptor descriptor) {
            return new JspMacroDescriptor(descriptor, locale);
        }
    };
    return Lists.transform(descs, function);
}

From source file:edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty.java

/**
 * Sorts the object property statements taking into account the sort order.
 */// ww w.  j  a  v  a 2  s .c om
public static List<ObjectPropertyStatement> sortObjectPropertyStatementsForDisplay(ObjectProperty prop,
        List objPropStmtsList) {

    if (objPropStmtsList == null) {
        log.error("incoming object property statement list is null; " + "returning null");
        return null;
    }
    if (objPropStmtsList.size() < 2) { // no need to sort
        return objPropStmtsList;
    }

    String tmpDirection = prop.getDomainEntitySortDirection();
    // Valid values are "desc" and "asc";
    // anything else will default to ascending.
    final boolean ascending = !"desc".equalsIgnoreCase(tmpDirection);

    String objIndivSortPropURI = prop.getObjectIndividualSortPropertyURI();
    if (prop.getObjectIndividualSortPropertyURI() == null
            || prop.getObjectIndividualSortPropertyURI().length() == 0) {
        log.debug("objectIndividualSortPropertyURI is null or blank " + "so sorting by name ");

        Comparator fieldComp = new Comparator() {

            public final int compare(Object o1, Object o2) {
                ObjectPropertyStatement e2e1 = (ObjectPropertyStatement) o1,
                        e2e2 = (ObjectPropertyStatement) o2;
                Individual e1, e2;
                e1 = e2e1 != null ? e2e1.getObject() : null;
                e2 = e2e2 != null ? e2e2.getObject() : null;

                Object val1 = null, val2 = null;
                if (e1 != null) {
                    val1 = e1.getName();
                } else {
                    log.debug("PropertyWebapp.sortObjectPropertiesForDisplay() "
                            + "passed object property statement with no range entity.");
                }
                if (e2 != null) {
                    val2 = e2.getName();
                } else {
                    log.debug("PropertyWebapp.sortObjectPropertyStatementsForDisplay "
                            + "passed object property statement with no range entity.");
                }
                int rv = 0;
                try {
                    if (val1 instanceof String) {
                        if (val2 == null) {
                            rv = -1;
                        } else {
                            Collator collator = Collator.getInstance();
                            rv = collator.compare(((String) val1), ((String) val2));
                        }
                    } else if (val1 instanceof Date) {
                        DateTime dt1 = new DateTime((Date) val1);
                        DateTime dt2 = new DateTime((Date) val2);
                        rv = dt1.compareTo(dt2);
                    } else {
                        rv = 0;
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }

                if (ascending) {
                    return rv;
                } else {
                    return rv * -1;
                }
            }
        };
        try {
            Collections.sort(objPropStmtsList, fieldComp);
        } catch (Exception e) {
            log.error("Exception sorting object property statements for object property " + prop.getURI());
        }
    } else { // sort by specified range entity data property value instead of a property having a get() method in Individual.java
        log.debug("using data property " + prop.getObjectIndividualSortPropertyURI()
                + " to sort related entities");
        final String objIndSortPropURI = prop.getObjectIndividualSortPropertyURI();
        Comparator dpComp = new Comparator() {
            final String cDatapropURI = objIndSortPropURI;

            public final int compare(Object o1, Object o2) {
                ObjectPropertyStatement e2e1 = (ObjectPropertyStatement) o1,
                        e2e2 = (ObjectPropertyStatement) o2;
                Individual e1, e2;
                e1 = e2e1 != null ? e2e1.getObject() : null;
                e2 = e2e2 != null ? e2e2.getObject() : null;

                Object val1 = null, val2 = null;
                if (e1 != null) {
                    try {
                        List<DataPropertyStatement> dataPropertyStatements = e1.getDataPropertyStatements();
                        for (DataPropertyStatement dps : dataPropertyStatements) {
                            if (cDatapropURI.equals(dps.getDatapropURI())) {
                                if (dps.getData() != null && dps.getData().trim().length() > 0) {
                                    if (XSDDatatype.XSDint.getURI().equals(dps.getDatatypeURI())
                                            || XSDDatatype.XSDinteger.getURI().equals(dps.getDatatypeURI())) {
                                        val1 = Integer.parseInt(dps.getData());
                                    } else {
                                        val1 = dps.getData();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else
                    log.debug(
                            "PropertyWebapp.sortObjectPropertiesForDisplay passed object property statement with no range entity.");

                if (e2 != null) {
                    try {
                        List<DataPropertyStatement> dataPropertyStatements = e2.getDataPropertyStatements();
                        for (DataPropertyStatement dps : dataPropertyStatements) {
                            if (cDatapropURI.equals(dps.getDatapropURI())) {
                                if (dps.getData() != null && dps.getData().trim().length() > 0) {
                                    if (XSDDatatype.XSDint.getURI().equals(dps.getDatatypeURI())
                                            || XSDDatatype.XSDinteger.getURI().equals(dps.getDatatypeURI())) {
                                        val2 = Integer.parseInt(dps.getData());
                                    } else {
                                        val2 = dps.getData();
                                    }
                                }
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    log.debug(
                            "PropertyWebapp.sortObjectPropertyStatementsForDisplay passed object property statement with no range entity.");
                }
                int rv = 0;
                try {
                    if (val1 == null && val2 == null) {
                        rv = 0;
                    } else if (val1 == null) {
                        rv = 1;
                    } else if (val2 == null) {
                        rv = -1;
                    } else {
                        if (val1 instanceof String) {
                            Collator collator = Collator.getInstance();
                            rv = collator.compare(((String) val1), ((String) val2)); //was rv = ((String)val1).compareTo((String)val2);
                        } else if (val1 instanceof Date) {
                            DateTime dt1 = new DateTime((Date) val1);
                            DateTime dt2 = new DateTime((Date) val2);
                            rv = dt1.compareTo(dt2);
                        } else if (val1 instanceof Integer) {
                            rv = ((Integer) val1) - ((Integer) val2);
                        } else {
                            rv = 0;
                        }
                    }
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }

                if (!ascending) {
                    rv = rv * -1;
                }

                // sort alphabetically by name if have same dataproperty value
                if (rv == 0) {
                    String nameValue1 = (e1.getName() != null) ? e1.getName() : "";
                    String nameValue2 = (e2.getName() != null) ? e2.getName() : "";
                    rv = Collator.getInstance().compare(nameValue1, nameValue2);
                }

                return rv;
            }
        };
        try {
            Collections.sort(objPropStmtsList, dpComp);
        } catch (Exception e) {
            log.error("Exception sorting object property statements " + "for object property " + prop.getURI(),
                    e);
        }
    }
    return objPropStmtsList;
}

From source file:com.eutectoid.dosomething.picker.GraphObjectAdapter.java

private static int compareGraphObjects(JSONObject a, JSONObject b, Collection<String> sortFields,
        Collator collator) {
    for (String sortField : sortFields) {
        String sa = a.optString(sortField);
        String sb = b.optString(sortField);

        if (sa != null && sb != null) {
            int result = collator.compare(sa, sb);
            if (result != 0) {
                return result;
            }//from   w w  w.  j a v  a2  s.  co m
        } else if (!(sa == null && sb == null)) {
            return (sa == null) ? -1 : 1;
        }
    }
    return 0;
}

From source file:com.facebook.widget.GraphObjectAdapter.java

private static int compareGraphObjects(GraphObject a, GraphObject b, Collection<String> sortFields,
        Collator collator) {
    for (String sortField : sortFields) {
        String sa = (String) a.getProperty(sortField);
        String sb = (String) b.getProperty(sortField);

        if (sa != null && sb != null) {
            int result = collator.compare(sa, sb);
            if (result != 0) {
                return result;
            }/*from   w  w w. j  a  v  a2s .  co  m*/
        } else if (!(sa == null && sb == null)) {
            return (sa == null) ? -1 : 1;
        }
    }
    return 0;
}

From source file:org.sakaiproject.umem.tool.ui.UserListBean.java

public static final Comparator<UserRow> getUserRowComparator(final String fieldName,
        final boolean sortAscending, final Collator collator) {
    return new Comparator<UserRow>() {

        public int compare(UserRow o1, UserRow o2) {
            UserRow r1 = (UserRow) o1;//from  w  ww  . j  a  va2s.c  om
            UserRow r2 = (UserRow) o2;
            try {
                if (fieldName.equals(SORT_USER_NAME)) {
                    String s1 = r1.getUserName();
                    String s2 = r2.getUserName();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                } else if (fieldName.equals(SORT_USER_ID)) {
                    String s1 = r1.getUserEID();
                    String s2 = r2.getUserEID();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                } else if (fieldName.equals(SORT_USER_EMAIL)) {
                    String s1 = r1.getUserEmail();
                    String s2 = r2.getUserEmail();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                } else if (fieldName.equals(SORT_USER_TYPE)) {
                    String s1 = r1.getUserType();
                    String s2 = r2.getUserType();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                } else if (fieldName.equals(SORT_USER_AUTHORITY)) {
                    String s1 = r1.getAuthority();
                    String s2 = r2.getAuthority();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                } else if (fieldName.equals(SORT_USER_CREATED_ON)) {
                    String s1 = r1.getCreatedOn();
                    String s2 = r2.getCreatedOn();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                } else if (fieldName.equals(SORT_USER_MODIFIED_ON)) {
                    String s1 = r1.getModifiedOn();
                    String s2 = r2.getModifiedOn();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                } else if (fieldName.equals(SORT_INTERNAL_USER_ID)) {
                    String s1 = r1.getUserID();
                    String s2 = r2.getUserID();
                    int res = collator.compare(s1 != null ? s1.toLowerCase() : "",
                            s2 != null ? s2.toLowerCase() : "");
                    if (sortAscending)
                        return res;
                    else
                        return -res;
                }
            } catch (Exception e) {
                LOG.warn("Error occurred while sorting by: " + fieldName, e);
            }
            return 0;
        }
    };
}

From source file:net.pms.dlna.RootFolder.java

private static boolean areNamesEqual(String aThis, String aThat) {
    Collator collator = Collator.getInstance(Locale.getDefault());
    collator.setStrength(Collator.PRIMARY);
    int comparison = collator.compare(aThis, aThat);
    return (comparison == 0);
}

From source file:org.jboss.dashboard.ui.formatters.FileNavigationTreeFormatter.java

protected void sortDirectories(List childDirectories) {
    final Collator collator = Collator.getInstance(getLocale());
    Collections.sort(childDirectories, new Comparator() {
        public int compare(Object o1, Object o2) {
            return collator.compare(((FileObject) o1).getName().getBaseName(),
                    ((FileObject) o2).getName().getBaseName());
        }//ww w  .j  a  v a2  s . c o  m
    });
}

From source file:edu.cornell.mannlib.vitro.webapp.beans.Ontology.java

public int compareTo(Ontology o2) {
    Collator collator = Collator.getInstance();
    if (o2 == null) {
        log.error("Ontology NULL in DisplayComparator()");
        return 0;
    }/*  www .j av a2  s  .c  o  m*/
    return collator.compare(this.getName(), o2.getName());
}

From source file:org.jboss.dashboard.ui.formatters.FileNavigationFileListFormatter.java

protected void sortFiles(List files) {
    final Collator collator = Collator.getInstance(getLocale());
    Collections.sort(files, new Comparator() {
        public int compare(Object o1, Object o2) {
            return collator.compare(((FileObject) o1).getName().getBaseName(),
                    ((FileObject) o2).getName().getBaseName());
        }/*  w  w  w.java  2  s.c  o  m*/
    });
}

From source file:edu.cornell.mannlib.vitro.webapp.beans.PropertyGroup.java

/**
 * Sorts PropertyGroup objects by group rank, then alphanumeric.
 * @author bdc34 modified by jc55, bjl23
 *///w w w .  ja  v  a 2  s.  c  o m
public int compareTo(PropertyGroup o2) {
    Collator collator = Collator.getInstance();
    if (o2 == null) {
        log.error("object NULL in DisplayComparator()");
        return 0;
    }
    int diff = (this.getDisplayRank() - o2.getDisplayRank());
    if (diff == 0) {
        return collator.compare(this.getName(), o2.getName());
    }
    return diff;
}