使用 Java TableRowSorter 对长整型进行排序

发布于 2024-09-08 04:33:10 字数 125 浏览 3 评论 0原文

我有一个处理大量数字的表,我希望对其进行排序。该表在显示数字时没有问题,但 TableRowSorter 似乎将数字视为整数,因此当大值超过最大整数值时,它们会错误地排序。如何使用 TableRowSorter 对 Long 进行排序?

I'm have a table that handles large numbers, and I want it to be sorted. The table has no problem displaying the numbers, but TableRowSorter seems to treat the numbers as Integers, thus sorting large values incorrectly when they exceed the maximum Integer value. How can I use TableRowSorter to sort Longs?

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

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

发布评论

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

评论(3

以往的大感动 2024-09-15 04:33:10

但是TableRowSorter似乎对待
数字为整数,从而排序
较大的值不正确时
超过最大整数值

那是因为默认情况下添加到表中的数据被视为字符串。您需要重写 TableModel 的 getColumnClass() 方法以返回列中数据的正确类,并且行排序器将为该类型的数据使用适当的比较器。

but TableRowSorter seems to treat the
numbers as Integers, thus sorting
large values incorrectly when they
exceed the maximum Integer value

Thats because by default data added to a table is treated as a String. You need to override the getColumnClass() method of your TableModel to return the proper Class of the data in the column and the row sorter will use the appropriate Comparator for that type of data.

风追烟花雨 2024-09-15 04:33:10

您可以尝试 指定您自己的比较器,它将提供如何比较值的详细信息。

You could try to specify your own comparator that will provide details of how to compare the values.

柒夜笙歌凉 2024-09-15 04:33:10

将这些长数字视为字符串,表行排序器将对它们进行正确排序。

Treat these long numbers as strings and table row sorter will sort them properly.

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