Alphanum Sorting
by David Qiao • December 7, 2011 • Java and Swing, JIDE
While working on an issue related to the file encoding, I am trying to adjust the file encoding settings in IntelliJ IDEA. I noticed something not quite right about the sort order of the encodings. Naturally the ISO-8859-2 to ISO-8859-10 should appear before ISO-8859-13 and -15. UTF-8 should also appear before UTF-16 and UTF-32. Clearly IntelliJ is doing a straight alphabetic sorting.
We actually resolved this issue a while ago in the open source JIDE Common Layer. There is a comparator called AlphanumComparator under jidesoft.comparator package. (courtesy to David Koelle). As long as you use this comparator, it will sort the strings with numbers correctly according to what users would expect. See below. The list on the left is the straight alphabetic sorting. The list on the right is using the alphanum sorting.
If you are using SortableComboBoxModel or SortableListModel, you can simply call the code below to enable the alphanum sorting.
sortableComboBoxModel.setComparatorContext(AlphanumComparator.CONTEXT);
and
sortableListModel.setComparatorContext(AlphanumComparator.CONTEXT);



