# Sort in Cyrilic order

• April 21st, 2010, 03:50 AM
cselic
Sort in Cyrilic order
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 there is any other way to sort words in cyrilic order, explain me how it
can be done.

Code :

```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; } }```
• April 24th, 2010, 09:13 AM
Brian
Re: Sort in Cyrilic order
Why don't you use string comparison operators, instead of making your own comparative functions by the way???
• May 5th, 2010, 07:17 AM
cselic
Re: Sort in Cyrilic order
Give me an example of comparison operators.
• May 5th, 2010, 03:08 PM
dlorde
Re: Sort in Cyrilic order
If you want a lexicographic sort, just use String.compareTo(..). If that's not suitable, you could look at CharSet encodings and comparison using StringBuffer.