简体   繁体   English

使用Java TableRowSorter对长整数排序

[英]Sorting Long integers with Java TableRowSorter

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. 该表显示数字没有问题,但是TableRowSorter似乎将数字视为Integer,因此当它们超过最大Integer值时,它们将错误地排序。 How can I use TableRowSorter to sort Longs? 如何使用TableRowSorter对Longs进行排序?

but TableRowSorter seems to treat the numbers as Integers, thus sorting large values incorrectly when they exceed the maximum Integer value 但是TableRowSorter似乎将数字视为Integer,因此当它们超过最大Integer值时,将不正确地对大值进行排序

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. 您需要重写TableModel的getColumnClass()方法以返回列中数据的正确类,并且行排序器将为该类型的数据使用适当的Comparator。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM