Hi. I've written a comparator for cyrilic sort.
Problem is when I try to compare words its happend that for example
words with first letter "J" are before words with first letter "A".
What's wrong with my code?
If, already exist comparator for cyrilic sort, please tell me about it, or
if there is any other way to sort words in cyrilic order, explain me how it
can be done.
import java.util.Comparator; class MyComparator implements Comparator<String> { private String cyrilic = "АаБбВвГгДдЂђЕеЖжЗзИиЈјКкЛлЉљМмНнЊњОоПпРрСсТтЋћУуФфХхЦцЧчЏџШш"; private static final int NUMBER_OF_CYRILIC_LETTERS = 60; public int compare(String word1, String word2) { String firstWord, secondWord; String shorterWord, longerWord; int shorterFirstWord = -1; firstWord = word1; secondWord = word2; if(firstWord.length() < secondWord.length()) { shorterWord = firstWord; longerWord = secondWord; shorterFirstWord = -1; } else { shorterWord = secondWord; longerWord = firstWord; shorterFirstWord = 1; } if(shorterWord.equals(longerWord.substring(0, shorterWord.length()-1))) { return shorterFirstWord; } for(int i = 0; i < shorterWord.length(); i++) { for(int j = 0; j < NUMBER_OF_CYRILIC_LETTERS; j++) { if((firstWord.charAt(i) == cyrilic.charAt(j)) && (secondWord.charAt(i) != cyrilic.charAt(j))) return -1; else if ((firstWord.charAt(i) != cyrilic.charAt(j)) && (secondWord.charAt(i) == cyrilic.charAt(j))) return 1; else return 0; } } return 0; } public boolean equals(Object obj) { String rec = (String)obj; if(rec.equals(this)) return true; return false; } }