Ger*_*rry 12 c# linq sorting list
我有一个包含字符串的列表"Others".我得到这个列表下拉.我按字母顺序排序这个列表.但我"Others"总是需要在列表的末尾.我不想在排序后添加这个元素,这是一个解决方案.有没有其他方法可以像使用.Sort()方法的自定义比较器一样.我试过下面但没有解决方案.
public class EOComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
if (x.ToLower().Contains("other"))
{
return -1;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
并将其称为:
EOComparer c = new EOComparer();
a.Sort((x, y) => c.Compare(x.OptionValue, y.OptionValue));
return a;
Run Code Online (Sandbox Code Playgroud)
如果有可能请帮忙.
Car*_*ine 21
使用这个逻辑
List<string> l = new List<string>{ "z", "y", "x", "other", "b", "a", "c" };
var result = l.OrderBy(i => i == "other").ThenBy(i => i).ToList();
result.ForEach(Console.WriteLine);
Run Code Online (Sandbox Code Playgroud)
输出:
abcxyz其他
如果你想other成为列表的顶部
var result = l.OrderBy(i => i != "other").ThenBy(i => i).ToList();
Run Code Online (Sandbox Code Playgroud)
输出:
其他abcxyz
wes*_*ton 10
这是一个很好的答案,但我想我会修复你的比较器:
测试:
[TestCase(new string[0], new string[0])]
[TestCase(new[] { "a" }, new[] { "a" })]
[TestCase(new[] { "a", "b" }, new[] { "a", "b" })]
[TestCase(new[] { "b", "a" }, new[] { "a", "b" })]
[TestCase(new[] {"others"}, new[] {"others"})]
[TestCase(new[] {"a", "others"}, new[] {"a", "others"})]
[TestCase(new[] {"others", "a"}, new[] {"a", "others"})]
[TestCase(new[] {"others", "x"}, new[] {"x", "others"})]
[TestCase(new[] {"Others", "x"}, new[] {"x", "Others"})]
[TestCase(new[] { "othersz", "others" }, new[] { "othersz", "others" })]
[TestCase(new[] {"z", "y", "x", "others", "b", "a", "c"},
new[] {"a", "b", "c", "x", "y", "z", "others"})]
public void CanSortWithOthersAtEnd(string[] input, string[] expectedSorted)
{
var a = new List<string>(input);
var c = new EOComparer();
a.Sort(c.Compare);
CollectionAssert.AreEqual(expectedSorted, a);
}
Run Code Online (Sandbox Code Playgroud)
比较器:
public sealed class EOComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (IsOthers(x)) return 1;
if (IsOthers(y)) return -1;
return string.Compare(x, y, StringComparison.Ordinal);
}
private static bool IsOthers(string str)
{
return string.Compare(str, "others", StringComparison.OrdinalIgnoreCase) == 0;
}
}
Run Code Online (Sandbox Code Playgroud)
注意我的使用如何string.Compare避免所有== null检查.以及如何StringComparison.OrdinalIgnoreCase避免.ToLower()并因此避免创建字符串的副本.
| 归档时间: |
|
| 查看次数: |
4227 次 |
| 最近记录: |