Skip to content

Commit 5eb64da

Browse files
Fix: Honor CompareOptions and Culture in RangeSortOptions (#2345)
Fixed a bug where the internal string comparer in EPPlus sorting hard-coded StringComparison.CurrentCulture, effectively ignoring the CompareOptions (like Ordinal or IgnoreCase) provided by the user. - Updated EPPlusSortComparerBase to use Culture and CompOptions properties. - Added unit tests to verify linguistic and ordinal case-sensitivity. Co-authored-by: Lieven De Foor <lieven.de.foor@tvh.com>
1 parent 501bb80 commit 5eb64da

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/EPPlus/Sorting/Internal/EPPlusSortComparerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected int CompareObjects(object x1, object y1)
7676
{
7777
var s1 = x1 == null ? "" : x1.ToString();
7878
var s2 = y1 == null ? "" : y1.ToString();
79-
ret = string.Compare(s1, s2, StringComparison.CurrentCulture);
79+
ret = string.Compare(s1, s2, Culture ?? CultureInfo.CurrentCulture, CompOptions);
8080
}
8181
else
8282
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using OfficeOpenXml;
3+
using OfficeOpenXml.Sorting;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Globalization;
7+
using System.Linq;
8+
9+
namespace EPPlusTest.Sorting
10+
{
11+
[TestClass]
12+
public class SortCaseSensitivityTests : TestBase
13+
{
14+
[TestMethod]
15+
public void ShouldRespectCaseSensitivity()
16+
{
17+
using (var package = new ExcelPackage())
18+
{
19+
var sheet = package.Workbook.Worksheets.Add("SortTest");
20+
21+
// Data: APPLE then apple
22+
sheet.Cells["A1"].Value = "APPLE";
23+
sheet.Cells["A2"].Value = "apple";
24+
25+
// 1. Case-Insensitive Sort (Should preserve original order APPLE, apple)
26+
var options = RangeSortOptions.Create();
27+
options.CompareOptions = CompareOptions.IgnoreCase;
28+
options.SortBy.Column(0);
29+
sheet.Cells["A1:A2"].Sort(options);
30+
31+
Assert.AreEqual("APPLE", sheet.Cells["A1"].Text, "Insensitive sort failed to preserve order");
32+
Assert.AreEqual("apple", sheet.Cells["A2"].Text);
33+
34+
// 2. Case-Sensitive Sort (Should flip to apple, APPLE because a < A in linguistic sort)
35+
options = RangeSortOptions.Create();
36+
options.CompareOptions = CompareOptions.None; // linguistic sensitive
37+
options.SortBy.Column(0);
38+
sheet.Cells["A1:A2"].Sort(options);
39+
40+
Assert.AreEqual("apple", sheet.Cells["A1"].Text, "Sensitive sort failed to flip order");
41+
Assert.AreEqual("APPLE", sheet.Cells["A2"].Text);
42+
}
43+
}
44+
45+
[TestMethod]
46+
public void ShouldRespectOrdinalCaseSensitivity()
47+
{
48+
using (var package = new ExcelPackage())
49+
{
50+
var sheet = package.Workbook.Worksheets.Add("SortTestOrdinal");
51+
52+
// Data: apple then APPLE
53+
sheet.Cells["A1"].Value = "apple";
54+
sheet.Cells["A2"].Value = "APPLE";
55+
56+
// Ordinal Sort (Binary): A (65) < a (97). So APPLE should be first.
57+
var options = RangeSortOptions.Create();
58+
options.CompareOptions = CompareOptions.Ordinal;
59+
options.SortBy.Column(0);
60+
sheet.Cells["A1:A2"].Sort(options);
61+
62+
Assert.AreEqual("APPLE", sheet.Cells["A1"].Text, "Ordinal sort failed to put uppercase first");
63+
Assert.AreEqual("apple", sheet.Cells["A2"].Text);
64+
}
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)