public int Compare(LanguageInfo x, LanguageInfo y)
            {
                if (x.LanguageTag == y.LanguageTag)
                {
                    return(0);
                }
                if (!x.Names[0].Equals(y.Names[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    // Favor ones where some language matches to solve BL-1141
                    if (x.Names[0].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(-1);
                    }
                    if (y.Names[0].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(1);
                    }
                    if (x.Names.Count > 1 && x.Names[1].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(-1);
                    }
                    if (y.Names.Count > 1 && y.Names[1].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(1);
                    }
                }

                if (x.LanguageTag.Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(-1);
                }
                if (y.LanguageTag.Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(1);
                }

                if (IetfLanguageTag.GetLanguagePart(x.LanguageTag).Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(-1);
                }
                if (IetfLanguageTag.GetLanguagePart(y.LanguageTag).Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(1);
                }

                int res = string.Compare(x.Names[0], y.Names[0], StringComparison.InvariantCultureIgnoreCase);

                if (res != 0)
                {
                    return(res);
                }

                return(string.Compare(x.LanguageTag, y.LanguageTag, StringComparison.InvariantCultureIgnoreCase));
            }
Beispiel #2
0
            public int Compare(LanguageInfo x, LanguageInfo y)
            {
                if (x.LanguageTag == y.LanguageTag)
                {
                    return(0);
                }
                if (!x.Names[0].Equals(y.Names[0], StringComparison.InvariantCultureIgnoreCase))
                {
                    // Favor ones where some language matches to solve BL-1141
                    if (x.Names[0].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(-1);
                    }
                    if (y.Names[0].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(1);
                    }
                    if (x.Names.Count > 1 && x.Names[1].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(-1);
                    }
                    if (y.Names.Count > 1 && y.Names[1].Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(1);
                    }
                }

                if (x.LanguageTag.Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(-1);
                }
                if (y.LanguageTag.Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(1);
                }

                if (IetfLanguageTag.GetLanguagePart(x.LanguageTag).Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(-1);
                }
                if (IetfLanguageTag.GetLanguagePart(y.LanguageTag).Equals(_searchString, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(1);
                }

                // Use the "editing distance" relative to the search string to sort by the primary name.
                // (But we don't really care once the editing distance gets very large.)
                // See https://silbloom.myjetbrains.com/youtrack/issue/BL-5847 for motivation.
                // Timing tests indicate that 1) calculating these distances doesn't slow down the sorting noticeably
                // and 2) caching these distances in a dictionary also doesn't speed up the sorting noticeably.
                var xDistance    = ApproximateMatcher.EditDistance(_lowerSearch, x.Names[0].ToLowerInvariant(), 25, false);
                var yDistance    = ApproximateMatcher.EditDistance(_lowerSearch, y.Names[0].ToLowerInvariant(), 25, false);
                var distanceDiff = xDistance - yDistance;

                if (distanceDiff != 0)
                {
                    return(distanceDiff);
                }

                // If the editing distances for the primary names are the same, sort by the primary name.
                int res = string.Compare(x.Names[0], y.Names[0], StringComparison.InvariantCultureIgnoreCase);

                if (res != 0)
                {
                    return(res);
                }

                return(string.Compare(x.LanguageTag, y.LanguageTag, StringComparison.InvariantCultureIgnoreCase));
            }