我正在用Java构建一个用于项目的小型内存文件系统,我目前正在使用二进制搜索来定位节点,但是当我在查找错误时,我注意到带有大写字母的节点将被排序到列表的开头因为他们在Unicode中的地位。 这仍然可以用于查找具有二进制搜索的节点,但是它们应该表示按字母顺序排序的列表,这是不可接受的,例如:
Hosts.txt abc home settings.txt我在想什么是最简单的解决方案(理论上)将字符串放在字符集中,字符布局如下:
aAbBcCdDeEfFgGhHiIjJkKlLmM... and so on with special characters and such included有没有办法轻松地做到这一点或者更好的方式?
I'm building a small in-memory file system in Java for a project, I'm currently using a binary search to locate nodes but when I was looking for bugs I noticed that nodes with capitals would be sorted to the beginning of the list because of their position in Unicode. This still works fine for finding nodes with binary search but while they should represent an alphabetically sorted list this is not acceptable, for example:
Hosts.txt abc home settings.txtWhat I'm thinking would be the easiest solution (theoretically) would be to have the strings in a charset with the characters laid out something like this:
aAbBcCdDeEfFgGhHiIjJkKlLmM... and so on with special characters and such includedIs there some way to do this easily or perhaps a better way?
最满意答案
你应该定义自己的比较器:
public class MyStringComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { int caseIgnoreResult = s1.compareToIgnoreCase(s2); if (caseIgnoreResult == 0) { return s1.compareTo(s2); } else { return caseIgnoreResult; } } }我希望这有帮助。
You should define your own comparator:
public class MyStringComparator implements Comparator<String> { @Override public int compare(String s1, String s2) { int caseIgnoreResult = s1.compareToIgnoreCase(s2); if (caseIgnoreResult == 0) { return s1.compareTo(s2); } else { return caseIgnoreResult; } } }I hope this helps.
更多推荐
发布评论