Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/EPPlus/Sorting/Internal/EPPlusSortComparerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected int CompareObjects(object x1, object y1)
{
var s1 = x1 == null ? "" : x1.ToString();
var s2 = y1 == null ? "" : y1.ToString();
ret = string.Compare(s1, s2, StringComparison.CurrentCulture);
ret = string.Compare(s1, s2, Culture ?? CultureInfo.CurrentCulture, CompOptions);
}
else
{
Expand Down
67 changes: 67 additions & 0 deletions src/EPPlusTest/Sorting/SortCaseSensitivityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OfficeOpenXml;
using OfficeOpenXml.Sorting;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace EPPlusTest.Sorting
{
[TestClass]
public class SortCaseSensitivityTests : TestBase
{
[TestMethod]
public void ShouldRespectCaseSensitivity()
{
using (var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("SortTest");

// Data: APPLE then apple
sheet.Cells["A1"].Value = "APPLE";
sheet.Cells["A2"].Value = "apple";

// 1. Case-Insensitive Sort (Should preserve original order APPLE, apple)
var options = RangeSortOptions.Create();
options.CompareOptions = CompareOptions.IgnoreCase;
options.SortBy.Column(0);
sheet.Cells["A1:A2"].Sort(options);

Assert.AreEqual("APPLE", sheet.Cells["A1"].Text, "Insensitive sort failed to preserve order");
Assert.AreEqual("apple", sheet.Cells["A2"].Text);

// 2. Case-Sensitive Sort (Should flip to apple, APPLE because a < A in linguistic sort)
options = RangeSortOptions.Create();
options.CompareOptions = CompareOptions.None; // linguistic sensitive
options.SortBy.Column(0);
sheet.Cells["A1:A2"].Sort(options);

Assert.AreEqual("apple", sheet.Cells["A1"].Text, "Sensitive sort failed to flip order");
Assert.AreEqual("APPLE", sheet.Cells["A2"].Text);
}
}

[TestMethod]
public void ShouldRespectOrdinalCaseSensitivity()
{
using (var package = new ExcelPackage())
{
var sheet = package.Workbook.Worksheets.Add("SortTestOrdinal");

// Data: apple then APPLE
sheet.Cells["A1"].Value = "apple";
sheet.Cells["A2"].Value = "APPLE";

// Ordinal Sort (Binary): A (65) < a (97). So APPLE should be first.
var options = RangeSortOptions.Create();
options.CompareOptions = CompareOptions.Ordinal;
options.SortBy.Column(0);
sheet.Cells["A1:A2"].Sort(options);

Assert.AreEqual("APPLE", sheet.Cells["A1"].Text, "Ordinal sort failed to put uppercase first");
Assert.AreEqual("apple", sheet.Cells["A2"].Text);
}
}
}
}