如何按多种标准对列表进行排序
本文关键字:排序 列表 何按多 标准 | 更新日期: 2023-09-27 18:02:56
我需要排序列表的节点与字段:距离和频率。我需要最小距离和最大频率的节点被放置在顶部。list. orderbydedending (frequency). thenby (distance) -不是那样的。
我想要得到OrderBy(distance)和orderbydescent (frequency)之间的平均值
的例子:
№ Distance frequency
1 6 15
2 4 10
3 5 3
我不知道如何解释得更清楚
我猜你是想按重量排序,就像
class Demo
{
public int Distance { get; set; }
public int Frequency { get; set; }
public override string ToString()
{
return string.Format("Distance:{0} Frequency:{1}", this.Distance, this.Frequency);
}
}
List<Demo> list = new List<Demo>
{
new Demo{ Distance=3, Frequency=15},
new Demo{ Distance=4, Frequency=17},
new Demo{ Distance=5, Frequency=3},
};
int[] weight = { 30, 70 };
var tmp = list.OrderByDescending(x => x.Distance * 0.3 + x.Frequency * 0.7);//there just a guess
foreach(var q in tmp)
{
Console.WriteLine(q);
}
您可以使用List.sort()和您创建的比较器来满足您的需求:)
public class ObjectBasedComparer : IComparer<ObjectType>
{
public int Compare(ObjectType a, ObjectType b)
{
//Your logic here...
}
}