Java Code Examples for java.text.Collator#compare()

The following examples show how to use java.text.Collator#compare() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GraphObjectAdapter.java    From Klyph with MIT License 6 votes vote down vote up
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;
            }
        } else if (!(sa == null && sb == null)) {
            return (sa == null) ? -1 : 1;
        }
    }
    return 0;
}
 
Example 2
Source File: TempBonusFacadeImpl.java    From pcgen with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public int compareTo(TempBonusFacadeImpl o)
{
	final Collator collator = Collator.getInstance();

	// Check sort keys first
	String key1 = this.getOriginObj().get(StringKey.SORT_KEY);
	if (key1 == null)
	{
		key1 = this.getOriginObj().getDisplayName();
	}
	String key2 = o.getOriginObj().get(StringKey.SORT_KEY);
	if (key2 == null)
	{
		key2 = o.getOriginObj().getDisplayName();
	}
	if (!key1.equals(key2))
	{
		return collator.compare(key1, key2);
	}
	return collator.compare(this.getOriginObj().getDisplayName(), o.getOriginObj().getDisplayName());
}
 
Example 3
Source File: GraphObjectAdapter.java    From facebook-api-android-maven with Apache License 2.0 6 votes vote down vote up
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;
            }
        } else if (!(sa == null && sb == null)) {
            return (sa == null) ? -1 : 1;
        }
    }
    return 0;
}
 
Example 4
Source File: GraphObjectAdapter.java    From android-skeleton-project with MIT License 6 votes vote down vote up
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;
            }
        } else if (!(sa == null && sb == null)) {
            return (sa == null) ? -1 : 1;
        }
    }
    return 0;
}
 
Example 5
Source File: LauncherModel.java    From LB-Launcher with Apache License 2.0 6 votes vote down vote up
public static final Comparator<AppInfo> getAppNameComparator() {
    final Collator collator = Collator.getInstance();
    return new Comparator<AppInfo>() {
        public final int compare(AppInfo a, AppInfo b) {
            if (a.user.equals(b.user)) {
                int result = collator.compare(a.title.toString().trim(),
                        b.title.toString().trim());
                if (result == 0) {
                    result = a.componentName.compareTo(b.componentName);
                }
                return result;
            } else {
                // TODO Need to figure out rules for sorting
                // profiles, this puts work second.
                return a.user.toString().compareTo(b.user.toString());
            }
        }
    };
}
 
Example 6
Source File: ConditionDTO.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(ConditionDTO that) {
    // including the id in the comparison so that the TreeSet that this
    // is stored in does not discard any entries just because their expressions
    // are equal
    final Collator collator = Collator.getInstance(Locale.US);
    final String thisCmpStr = getExpression() + "_" + getId();
    final String thatCmpStr = that.getExpression() + "_" + that.getId();
    return collator.compare(thisCmpStr, thatCmpStr);
}
 
Example 7
Source File: DocumentGroupImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo( DocumentGroupImpl o ) {
    Collator collator = Collator.getInstance();
    int res = collator.compare( displayName, o.displayName );
    if( 0 == res )
        res = collator.compare( name, o.name );
    return res;
}
 
Example 8
Source File: NativeString.java    From nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return (double)collator.compare(str, JSType.toString(that));
}
 
Example 9
Source File: NativeString.java    From jdk8u_nashorn with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 10
Source File: SortedStateUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a comparator for comparing state entry keys.
 *
 * @return comparator for comparing state entry keys
 */
public static Comparator<String> getKeyComparator() {
    final Collator collator = Collator.getInstance(Locale.US);
    return new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return collator.compare(s1, s2);
        }
    };
}
 
Example 11
Source File: Comparators.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public int compare(Object o1, Object o2)
{
	String key1 = getSortKey(o1);
	String key2 = getSortKey(o2);
	final Collator collator = Collator.getInstance();

	if (!key1.equals(key2))
	{
		return collator.compare(key1, key2);
	}
	return collator.compare(String.valueOf(o1), String.valueOf(o2));
}
 
Example 12
Source File: NativeString.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static Object localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return (double)collator.compare(str, JSType.toString(that));
}
 
Example 13
Source File: CollationTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Check whether the JVM suffers from this bug:
 * http://bugs.sun.com/view_bug.do?bug_id=4804273
 * If it does, the tests that use Swedish locale will fail.
 *
 * @return true if the bug is present, false otherwise
 */
private static boolean hasBuggySwedishLocale() {
    Collator c = Collator.getInstance(new Locale("sv"));
    if (c.compare("aa", "ab") < 0) {
        // OK, aa should be less than ab with Swedish collation
        return false;
    } else {
        // this is a bug
        return true;
    }
}
 
Example 14
Source File: NativeString.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.5.4.9 String.prototype.localeCompare (that)
 * @param self self reference
 * @param that comparison object
 * @return result of locale sensitive comparison operation between {@code self} and {@code that}
 */
@Function(attributes = Attribute.NOT_ENUMERABLE)
public static double localeCompare(final Object self, final Object that) {

    final String   str      = checkObjectToString(self);
    final Collator collator = Collator.getInstance(Global.getEnv()._locale);

    collator.setStrength(Collator.IDENTICAL);
    collator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);

    return collator.compare(str, JSType.toString(that));
}
 
Example 15
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static int compareStrings(boolean caseSensitive, String s1, String s2) {
    Collator collator = Collator.getInstance();
    collator.setStrength(caseSensitive ? Collator.TERTIARY : Collator.SECONDARY);
    return collator.compare(s1, s2);
}
 
Example 16
Source File: TwigPath.java    From idea-php-symfony2-plugin with MIT License 4 votes vote down vote up
@Override
public int compareTo(@NotNull TwigPath twigPath) {
    Collator collator = Collator.getInstance();
    collator.setStrength(Collator.SECONDARY);
    return collator.compare(this.getNamespace(), twigPath.getNamespace());
}
 
Example 17
Source File: ContentHostingComparator.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public int comparerLocalSensitive(String s1, String s2) {
	Collator c = Collator.getInstance();
	c.setStrength(Collator.PRIMARY);
	return c.compare(s1, s2);
}
 
Example 18
Source File: SitesImpl.java    From alfresco-remote-api with GNU Lesser General Public License v3.0 4 votes vote down vote up
private PagingResults<SiteInfo> getFavouriteSites(String userName, PagingRequest pagingRequest)
{
    final Collator collator = Collator.getInstance();

    final Set<SiteInfo> sortedFavouriteSites = new TreeSet<SiteInfo>(new Comparator<SiteInfo>()
    {
        @Override
        public int compare(SiteInfo o1, SiteInfo o2)
        {
            return collator.compare(o1.getTitle(), o2.getTitle());
        }
    });

    Map<String, Serializable> prefs = preferenceService.getPreferences(userName, FAVOURITE_SITES_PREFIX);
    for(Entry<String, Serializable> entry : prefs.entrySet())
    {
        boolean isFavourite = false;
        Serializable s = entry.getValue();
        if(s instanceof Boolean)
        {
            isFavourite = (Boolean)s;
        }
        if(isFavourite)
        {
            String siteShortName = entry.getKey().substring(FAVOURITE_SITES_PREFIX_LENGTH).replace(".favourited", "");
            SiteInfo siteInfo = siteService.getSite(siteShortName);
            if(siteInfo != null)
            {
                sortedFavouriteSites.add(siteInfo);
            }
        }
    }

    int totalSize = sortedFavouriteSites.size();
    final PageDetails pageDetails = PageDetails.getPageDetails(pagingRequest, totalSize);

    final List<SiteInfo> page = new ArrayList<SiteInfo>(pageDetails.getPageSize());
    Iterator<SiteInfo> it = sortedFavouriteSites.iterator();
    for(int counter = 0; counter < pageDetails.getEnd() && it.hasNext(); counter++)
    {
        SiteInfo favouriteSite = it.next();

        if(counter < pageDetails.getSkipCount())
        {
            continue;
        }

        if(counter > pageDetails.getEnd() - 1)
        {
            break;
        }

        page.add(favouriteSite);
    }

    return new PagingResults<SiteInfo>()
    {
        @Override
        public List<SiteInfo> getPage()
        {
            return page;
        }

        @Override
        public boolean hasMoreItems()
        {
            return pageDetails.hasMoreItems();
        }

        @Override
        public Pair<Integer, Integer> getTotalResultCount()
        {
            Integer total = Integer.valueOf(sortedFavouriteSites.size());
            return new Pair<Integer, Integer>(total, total);
        }

        @Override
        public String getQueryExecutionId()
        {
            return null;
        }
    };
}
 
Example 19
Source File: ContentHostingComparator.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
public int comparerLocalSensitive(String s1, String s2) {
	Collator c = Collator.getInstance();
	c.setStrength(Collator.PRIMARY);
	return c.compare(s1, s2);
}
 
Example 20
Source File: TrackListPanel.java    From gdx-soundboard with MIT License 4 votes vote down vote up
@Override
public int compare(Track o1, Track o2) {

    Collator comp = java.text.Collator.getInstance();
    return comp.compare(o1.toString(), o2.toString());
}