Reputation: 127
I'm not even sure where to start with this type of issue, and was hoping someone could point me in the right direction.
I have a list of items, each with four attributes. The attributes are weighed in favor, for instance, attribute 1 is more important than attribute 2, etc.
I was hoping to find an algorithm or equation of some sort to order these items based on these values. Thoughts?
Upvotes: 1
Views: 549
Reputation: 178461
You should use a regular sorting algorithm, only difference is your comparator, that saying x<y
is more complex, and should be something like the following pseudo code:
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
}
An alternative is to use stable sorting algorithm, and repeat the sorting for each attribute, starting from the least important, and ending with the most important.
Upvotes: 6
Reputation: 4272
If you want to sort (lexicographically) your things for example:
1234 1244 1312
You can use Radix algorith .
Upvotes: 1