开发者

How to group by a price confined to a dynamically generated ceiling?

开发者 https://www.devze.com 2023-01-12 04:23 出处:网络
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.

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 f开发者_如何学Ciguring 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消