views:

27

answers:

1

Hello,

I have a table of items with several properties but to keep it short, it has property price.

I want to group a List<Item> into groups of price ranges. The catch is that the price ranges (ceilings ...) have to be dynamically generated.

When the ceilings are static, things work fine (Using LINQ)

decimal[] ceilings = new decimal[] { 0, 10M, 100M, 500M, 5000M, 50000M };
var grouped = items.GroupBy( x => ceilings.First( y => y >= x.Price );

I'm in search of a good algorithm to generate the ceilings group on the fly based on the price of the items list.

I'm struggling with figuring out the step size though. I have a couple of ideas in my head such as finding the difference between the Max() and Min() of that list and using that to generate a list of ceilings.

Any ideas? Thank you very much in advance!

A: 

A bucket sort algorithm might do the trick.

Follow the literature. If my memory serves correctly there are algorithms for creating buckets that have the same number of entries. And of course, you can always just sort into (Max - Min)/N for N uniform sized buckets.

Rob