Ranking based on multiple values, any better way of doing it?

Welcome Forums General PowerShell Q&A Ranking based on multiple values, any better way of doing it?

This topic contains 2 replies, has 2 voices, and was last updated by

1 month ago.

  • Author
  • #121318

    Points: 195
    Helping Hand
    Rank: Participant

    This is more of a question on what would be (if any) the most efficient way of doing it.
    E.g. any accelerators or .Net classes/methods that would be better.
    Only looking for native .NET or PS functionality.

    I've currently solved the problem this way, included example data for easier testing.

    $dataSet = @()
    $dataSet += [PSCustomObject]@{DataName="Data1"; Value1=1; Value2=4; Value3=8;}
    $dataSet += [PSCustomObject]@{DataName="Data2"; Value1=2; Value2=5; Value3=3;}
    $dataSet += [PSCustomObject]@{DataName="Data3"; Value1=4; Value2=1; Value3=1;}
    $Value1 = $dataSet | sort Value1 | select DataName, Value1
    $Value2 = $dataSet | sort Value2 | select DataName, Value2
    $Value3 = $dataSet | sort Value3 | select DataName, Value3
    $rankList = @()
    foreach($d in $dataSet)
        $value1Rank = $Value1.DataName.IndexOf($d.DataName) + 1
        $value2Rank = $Value2.DataName.IndexOf($d.DataName) + 1
        $value3Rank = $Value3.DataName.IndexOf($d.DataName) + 1
        $rankList += [PSCustomObject]@{DataName = $d.DataName
                                       Value1Rank = $value1Rank
                                       Value2Rank = $value2Rank
                                       Value3Rank = $value3Rank
                                       TotalScore = $value1Rank + $value2Rank + $value3Rank

    The above is based on the lower the total score, the better (otherwise the sort would need to be reversed).
    So in this case Data3 would get the lowest score of 5.

    So the question is, is there a better way of solving the problem?

  • #123773

    Points: 111
    Helping Hand
    Rank: Participant

    This is what is returned from $rankList. I am unclear what your ultimate objective is. By which property or properties would you like to sort your output?

    DataName : Data1
    Value1Rank : 1
    Value2Rank : 2
    Value3Rank : 3
    TotalScore : 6

    DataName : Data2
    Value1Rank : 2
    Value2Rank : 3
    Value3Rank : 2
    TotalScore : 7

    DataName : Data3
    Value1Rank : 3
    Value2Rank : 1
    Value3Rank : 1
    TotalScore : 5

  • #123947

    Points: 195
    Helping Hand
    Rank: Participant

    It's not a sort question, the sort is just there to order the value from best to worst.
    The index-number will then tell in which position the value has later on.

    The data could be anything, it's not really important.
    E.g. it could be PizzaSize, Topping1, Topping2 🙂

    The code works for what I need.
    Meaning get a list of scores for each dataset based on the individual values they have.
    E.g. in the Pizza example it could be the most popular pizza/topping combo.

    The question is if there is a better way to come up to the same result.
    E.g. in C# you would probably use Linq instead of counting index-nubmers.

You must be logged in to reply to this topic.