如何按动态生成的上限范围内的价格进行分组?

发布于 2024-09-14 22:57:43 字数 547 浏览 0 评论 0原文

我有一个包含多个属性的 items 表格,但为了简单起见,它具有属性 price

我想将 List 分组为价格范围组。 问题是价格范围(上限……)必须动态生成。

当上限是静态的时,一切工作正常(使用 LINQ)

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

我正在寻找一种好的算法来根据 items 列表的价格动态生成上限组。

不过,我正在努力弄清楚步长。 我脑子里有几个想法,例如找出该列表的 Max()Min() 之间的差异,并使用它来生成上限列表。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-09-21 22:57:43

桶排序算法可能可以解决问题。

遵循文献。如果我的记忆正确的话,有一些算法可以创建具有相同条目数的存储桶。当然,您始终可以将 N 个统一大小的桶排序为 (Max - Min)/N。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文