d3.rank in descending order

I am trying to create a rank field in an array of objects using d3.rank

I want to create the rank based on descending order of the Value.

How can I do that?

Desired ranking in following

2022-c5-39.99%-rank1
2022-c3-22.58%-rank2
2022-c6-17.40%-rank3
2022-c2-6.39%-rank4
2022-c4-4.37%-rank5
2022-c1-1.66%-rank6

d3.rank(data, d => -d.value) works for numeric data

but you can do this:

d3.rank(values, d3.descending)

or this:

d3.rank(data, (a, b) => d3.descending(a.value, b.value))

1 Like

The fully adjusted code

 year.forEach(
        (b) => data.filter(x => x.Year === b)
        .map((a, i) => a.Rank =
            d3.rank(data.filter(x => x.Year === b),
                (a, b) => d3.descending(a.Value, b.Value),
                d => d.Value)[i] + 1
        )
    )
1 Like