diff --git a/Jint.Tests/Runtime/StringTests.cs b/Jint.Tests/Runtime/StringTests.cs index 6dff11818a..99bc9dd621 100644 --- a/Jint.Tests/Runtime/StringTests.cs +++ b/Jint.Tests/Runtime/StringTests.cs @@ -82,6 +82,14 @@ public void TemplateLiteralAsObjectKey() Assert.Equal("value", result["key"]); } + [Fact] + public void ShouldCompareWithLocale() + { + var engine = new Engine(); + Assert.Equal(1, engine.Evaluate("'王五'.localeCompare('张三')").AsInteger()); + Assert.Equal(-1, engine.Evaluate("'王五'.localeCompare('张三', 'zh-CN')").AsInteger()); + } + public static TheoryData GetLithuaniaTestsData() { return new StringTetsLithuaniaData().TestData(); diff --git a/Jint/Native/String/StringPrototype.cs b/Jint/Native/String/StringPrototype.cs index 7f636a4852..8b18c6fb58 100644 --- a/Jint/Native/String/StringPrototype.cs +++ b/Jint/Native/String/StringPrototype.cs @@ -765,7 +765,14 @@ private JsValue LocaleCompare(JsValue thisObject, JsValue[] arguments) var s = TypeConverter.ToString(thisObject); var that = TypeConverter.ToString(arguments.At(0)); - return string.CompareOrdinal(s.Normalize(NormalizationForm.FormKD), that.Normalize(NormalizationForm.FormKD)); + var culture = Engine.Options.Culture; + + if (arguments.Length > 1 && arguments[1].IsString()) + { + culture = CultureInfo.GetCultureInfo(arguments.At(1).AsString()); + } + + return culture.CompareInfo.Compare(s.Normalize(NormalizationForm.FormKD), that.Normalize(NormalizationForm.FormKD)); } ///