/*By Jiangong SUN*/
Today is thanks giving day! LOL :)
Firstly we have a list :
List<FeatureResult> FeaturesList = new List<FeatureResult>();
foreach (var feature in Model.Features)
{
for (int c = 0; c < feature.Value.Count(); c++)
{
FeaturesList.Add(feature.Value[c]);
}
}And then, we create a comparer :
public class FeatureResultComparer : IComparer<FeatureResult>
{
#region IComparer<FeatureResult> Members
///Compare two result
public int Compare(FeatureResult x, FeatureResult y)
{
return Level(x) - Level(y);
}
///add 1 if item has description
///add the length if item has multiple urls
private int Level(FeatureResult item)
{
int res = 0;
if (!string.IsNullOrEmpty(item.Description))
{
res += 1;
if (item.ImageUrl != null && item.ImageUrl.Length > 0)
res += item.ImageUrl.Length;
}
return res;
}
#endregion
}
Sort list by calling the comparer :
FeatureResultComparer featuresComparer = new FeatureResultComparer();
FeaturesList.Sort(featuresComparer);
FeaturesList.Reverse();Enjoy coding !

本文介绍了一种在编程中整理特征结果的方法,通过创建自定义比较器来实现列表排序,并最终反转排序结果。适用于需要对特征进行高效处理的场景。
615

被折叠的 条评论
为什么被折叠?



