How to rank elements of an array in VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic

Module CompetitionRanking

    '
    ' FUNCTION: GetRanks
    ' -------------------
    ' Computes competition-style ranks for a list of integers.
    '
    ' Ranking rules (Competition Ranking):
    ' ------------------------------------
    ' - Highest value gets rank 1
    ' - Equal values share the same rank
    ' - Next rank increases by the number of equal values
    '
    ' Steps:
    ' 1. Make a sorted copy of the list (descending)
    ' 2. Assign ranks to each unique value
    ' 3. Build a rank list matching the original order
    '
    Function GetRanks(arr As List(Of Integer)) As List(Of Integer)

        ' Step 1: Create a sorted copy (descending)
        Dim sorted As New List(Of Integer)(arr)
        sorted.Sort(Function(a, b) b.CompareTo(a))

        ' Step 2: Map each unique value to its rank
        Dim rankMap As New Dictionary(Of Integer, Integer)()
        Dim rank As Integer = 1

        For Each value In sorted
            ' Assign rank only once per unique value
            If Not rankMap.ContainsKey(value) Then
                rankMap(value) = rank
            End If
            rank += 1 ' next rank position
        Next

        ' Step 3: Build ranking list in original order
        Dim result As New List(Of Integer)()
        For Each value In arr
            result.Add(rankMap(value))
        Next

        Return result
    End Function

    '
    ' FUNCTION: PrintRanking
    ' -----------------------
    ' Prints the original list and its ranking.
    '
    Sub PrintRanking(arr As List(Of Integer), ranks As List(Of Integer))
        Console.Write("Original array: [ ")
        For Each v In arr
            Console.Write(v & " ")
        Next
        Console.WriteLine("]")

        Console.Write("Ranking array : [ ")
        For Each r In ranks
            Console.Write(r & " ")
        Next
        Console.WriteLine("]")
    End Sub

    ' MAIN PROGRAM
    ' ---------------------------------------------
    Sub Main()

        Dim arrayValues As New List(Of Integer) From {88, 4, 19, 4, 1, 31, 14, 1, 93, 17}

        ' Compute ranks
        Dim rankingArray As List(Of Integer) = GetRanks(arrayValues)

        PrintRanking(arrayValues, rankingArray)

    End Sub

End Module


'
' run:
'
' Original array: [ 88 4 19 4 1 31 14 1 93 17 ]
' Ranking array : [ 2 7 4 7 9 3 6 9 1 5 ]
'

 



answered Jun 21 by avibootz
0 votes
Imports System
Imports System.Collections.Generic
Imports System.Linq

Module CompetitionRanking

    '
    ' FUNCTION: GetRanks
    ' -------------------
    ' Computes competition-style ranks for a list of integers.
    '
    ' Ranking rules (Competition Ranking):
    ' ------------------------------------
    ' - Highest value gets rank 1
    ' - Equal values share the same rank
    ' - Next rank increases by the number of equal values
    '
    ' Steps:
    ' 1. Make a sorted copy of the list (descending)
    ' 2. Assign ranks to each unique value
    ' 3. Build a rank list matching the original order
    '
    Function GetRanks(arr As List(Of Integer)) As List(Of Integer)

        ' Step 1: Create a sorted copy (descending) using LINQ
        Dim sorted As List(Of Integer) =
            arr.OrderByDescending(Function(x) x).ToList()

        ' Step 2: Map each unique value to its rank
        Dim rankMap As New Dictionary(Of Integer, Integer)()
        Dim rank As Integer = 1

        For Each value In sorted
            ' Assign rank only once per unique value
            If Not rankMap.ContainsKey(value) Then
                rankMap(value) = rank
            End If
            rank += 1 ' next rank position
        Next

        ' Step 3: Build ranking list in original order
        Dim result As New List(Of Integer)()
        For Each value In arr
            result.Add(rankMap(value))
        Next

        Return result
    End Function

    '
    ' FUNCTION: PrintRanking
    ' -----------------------
    ' Prints the original list and its ranking.
    '
    Sub PrintRanking(arr As List(Of Integer), ranks As List(Of Integer))
        Console.Write("Original array: [ ")
        For Each v In arr
            Console.Write(v & " ")
        Next
        Console.WriteLine("]")

        Console.Write("Ranking array : [ ")
        For Each r In ranks
            Console.Write(r & " ")
        Next
        Console.WriteLine("]")
    End Sub

    ' MAIN PROGRAM
    ' ---------------------------------------------
    Sub Main()

        Dim arrayValues As New List(Of Integer) From {88, 4, 19, 4, 1, 31, 14, 1, 93, 17}

        ' Compute ranks
        Dim rankingArray As List(Of Integer) = GetRanks(arrayValues)

        PrintRanking(arrayValues, rankingArray)

    End Sub

End Module


'
' run:
'
' Original array: [ 88 4 19 4 1 31 14 1 93 17 ]
' Ranking array : [ 2 7 4 7 9 3 6 9 1 5 ]
'

 



answered Jun 21 by avibootz
...