[英]Remove Items from list with priority
Lets suppose i have a list and i want to remove accoding to the condition that if priority item is there then remove all smaller priority items.让我们假设我有一个列表,我想删除 accoding 条件是如果优先项目在那里然后删除所有较小的优先项目。 for eg
例如
Sortedarr[] =["world champion","Best performer","Best gold", "Best silver", "Best bronze", "regional"]
Conditions.状况。
the array would always be sorted so always gold>siilver>bronze would be the order.该数组将始终进行排序,因此始终是金>银>青铜的顺序。
My implementation is working but i dont think its the optimised way of doing it.我的实施工作正常,但我认为这不是优化的方式。
if(arr.Contains("Best gold")){
sortedArr.RemoveAll(x => ((string)x) == "Best silver"
|| ((string)x) == "Best Bronze" );
}
if (sortedArr.Contains("Best silver")) {
sortedArr.RemoveAll(x =>((string)x) == "Best bronze");
}
thank you in advance先感谢您
Your question is not clear.你的问题不清楚。 but you can use Array.IndexOf Method to check item is present:
但您可以使用Array.IndexOf 方法来检查项目是否存在:
List<string> array =(new string[] {"world champion", "Best performer", "Best gold", "Best silver", "Best bronze", "regional"}).ToList();
if (array.IndexOf("Best gold") > 0)
array.RemoveAll(m=>m.Contains("Best silver") || m.Contains("Best bronze"));
else if (array.IndexOf("Best silver") > 0)
array.RemoveAll(m => m.Contains("Best bronze"));
List<string> array =(new string[] {"world champion", "Best performer", "Best gold", "Best silver", "Best bronze", "regional"});
array.Contains("Best gold") ? array.RemoveAll(x => ((string)x) == "Best silver" || ((string)x) == "Best Bronze" )
: (array.Contains("Best silver") ?
array.RemoveAll(x =>((string)x) == "Best bronze")
: return array)
If it comes to sorting, I would recommend implementing own comparer for string, so implementing interface IComparer<string>
.如果涉及到排序,我建议为字符串实现自己的比较器,因此实现接口
IComparer<string>
。
Below simply checks wether compared string contains either gold, silver or bronze and gives desired output based on score of a medal.下面简单地检查比较字符串是否包含金、银或铜,并根据奖牌得分给出所需的 output。
In case of a tie, we fallback to chosen comparer, here StringComparer.OrdinalIgnoreCase
.在平局的情况下,我们回退到选择的比较器,这里是
StringComparer.OrdinalIgnoreCase
。
In case if none of the compared strings contain any of a medals, then we also fallback to chosen comparison type.如果比较的字符串都不包含任何奖牌,那么我们也会回退到选择的比较类型。
public class MedalComparer : IComparer<string>
{
private const string Gold = "gold";
private const string Silver = "silver";
private const string Bronze = "bronze";
// Here you should put all medals with respctive score
private static readonly Dictionary<int, string> Medals = new()
{
{1, Gold}, {2, Silver}, {3, Bronze},
};
private static readonly StringComparison StringComparison = StringComparison.OrdinalIgnoreCase;
private static readonly IComparer<string> _fallbackComparer = StringComparer.OrdinalIgnoreCase;
public int Compare(string x, string y)
{
return IsTie(x, y)
? _fallbackComparer.Compare(x, y)
: Evaluate(x, y) ?? _fallbackComparer.Compare(x, y);
}
private static bool IsTie(string x, string y)
{
return (x.Contains(Gold, StringComparison) && y.Contains(Gold, StringComparison)) ||
(x.Contains(Silver, StringComparison) && y.Contains(Silver, StringComparison)) ||
(x.Contains(Bronze, StringComparison) && y.Contains(Bronze, StringComparison));
}
private static int? Evaluate(string x, string y)
{
foreach (var medalKey in Medals.Keys.OrderBy(x => x))
{
if(x.Contains(Medals[medalKey], StringComparison))
{
return 1;
}
if (y.Contains(Medals[medalKey], StringComparison))
{
return -1;
}
}
return null;
}
}
Then you can pass it conveniently to OrderBy
method as shown below:然后就可以方便的传给
OrderBy
方法了,如下所示:
public static class Program
{
public static async Task Main(string[] args)
{
var testData = new[]{ "world champion","Best performer","Best gold", "Best silver", "Best bronze", "regional"};
var sorted = testData.OrderBy(x => x, new MedalComparer());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.