使用 Java TableRowSorter 对长整型进行排序
我有一个处理大量数字的表,我希望对其进行排序。该表在显示数字时没有问题,但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那是因为默认情况下添加到表中的数据被视为字符串。您需要重写 TableModel 的 getColumnClass() 方法以返回列中数据的正确类,并且行排序器将为该类型的数据使用适当的比较器。
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.
您可以尝试 指定您自己的比较器,它将提供如何比较值的详细信息。
You could try to specify your own comparator that will provide details of how to compare the values.
将这些长数字视为字符串,表行排序器将对它们进行正确排序。
Treat these long numbers as strings and table row sorter will sort them properly.