基于加权值确定项目顺序的算法

dxh*_*ns5 1 sorting algorithm

我甚至不确定从哪个类型的问题开始,并希望有人能指出我正确的方向.

我有一个项目列表,每个项目都有四个属性.对属性进行权衡,例如,属性1比属性2更重要,等等.

我希望找到一种算法或某种方程式来根据这些值来订购这些项目.思考?

ami*_*mit 6

您应该使用常规排序算法,只有差异是您的比较器,说x<y的更复杂,应该类似于以下伪代码:

compare(x,y) {
   if (x.attribute1 < y.attribute1) return -1
   if (x.attribute1 > y.attribute1) return 1
   if (x.attribute2 < y.attribute2) return -1
   if (x.attribute2 > y.attribute2) return 1
   if (x.attribute3 < y.attribute3) return -1
   if (x.attribute3 > y.attribute3) return 1
   if (x.attribute4 < y.attribute4) return -1
   if (x.attribute4 > y.attribute4) return 1
   return 0
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用稳定的排序算法,并重复每个属性的排序,从最不重要的开始,到最重要的结束.