按字段对 java beans 集合进行排序

发布于 2024-07-23 01:14:31 字数 196 浏览 5 评论 0原文

我有一个填充 JSF DataTable 的 java bean 集合,我正在尝试实现列排序。

我想根据所选字段对数组/集合进行排序。 我过去曾使用 Reflection 来实现此目的,但想找到一种更简洁的方法,使用 Commons BeanUtils 和/或 Collections,但似乎找不到任何示例。

谢谢 斯科蒂亚布

I have an collection of java beans that populate a JSF DataTable I'm trying to implement column sorting.

I'd like to sort the array/collection depending on the field selected. I have using Reflection in the past for this, but wanted to find a neater way of doing it, using Commons BeanUtils and/or Collections but can't seem to find any examples.

Thanks
Scottyab

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

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

发布评论

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

评论(2

仙女山的月亮 2024-07-30 01:14:31

实际上,经过一番尝试后,这就是我想到的,它似乎有效,

String sortColumn = (String)getRequestParam("sort_id");     
List<Quote> quotes = (List<Quote>)getSessionScope().get(SESS_SEARCH_RESULTS);           
Comparator fieldCompare = new org.apache.commons.beanutils.BeanComparator( sortColumn );
Collections.sort(quotes, fieldCompare );

现在只需要查看排序顺序:)

Actually after a bit of playing around here's what i come up with and it seems to work

String sortColumn = (String)getRequestParam("sort_id");     
List<Quote> quotes = (List<Quote>)getSessionScope().get(SESS_SEARCH_RESULTS);           
Comparator fieldCompare = new org.apache.commons.beanutils.BeanComparator( sortColumn );
Collections.sort(quotes, fieldCompare );

Just need to look at the sort order now :)

月野兔 2024-07-30 01:14:31

与您的具体问题没有立即相关,但请查看GlazedLists - 它使得在GUI上实现这些东西非常容易。

not immediately relevant to your specific question, but look at GlazedLists -- it makes implementing this stuff for GUI's really easy.

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