1

I have a set of unbound values stored in an array (it's an NSArray, if that helps). The values can range from 0 to infinity (in reality the max value is about 100,000,000).

I need to sort the values in my array into a scale of 0 to 20.

For example, if I have a set of 1000, 3123, 31, 31 would be 0, 1000 would be 10 and 3123 would be 20. The arrays vary in number of members and in max values, so I know if there are a lot of values with different ranges, a lot of those values would take up the same positions on the scale. Most of the time, the number of items will be way over 20 (the average number of items is about 100).

When the number of items is over 20, some of the items should have the same rating.

I need this for visual representations of songs' populatities per search queries, where those values are the global representations of the song's position on a global chart.

Is it possible to create such an algorithm?

Kristina
  • 15,859
  • 29
  • 111
  • 181

3 Answers3

2

If I understand you right you just want to sort your values ascending and map them to the range of 0 to 20? So you could just count how many values you have (e. g. 11), divide your range by this number - 1 (e. g. 20/(11 - 1) = 2) and assign each value the appropriate number (e. g. 0, 2, 4, 6, 8...)

Daniel
  • 1,527
  • 10
  • 13
0

Sort the array. Now multiply the index of each item with (20 / ([myArray count] - 1)); Note that count MUST be > 1. I guess you won't have more than 20 items?

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
0

Does the percentage like calculation help you? i.e use the max value as the 100% (i.e 20) and the rest mapped as percentage of the max value. For your ex:

[ 1000 3123 31 ]
(31/3123)*20 = 2
(1000/3123)*20 = 6 (or 7)
(3123/3123)*20 = 20

This way the final values (on 0-20 scale) would still have the distribution of the numbers in initial set.

Davinc
  • 39
  • 1