Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Sort in Cyrilic order

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    My Mood
    Nerdy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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, 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;
    	}
    }


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sort in Cyrilic order

    Why don't you use string comparison operators, instead of making your own comparative functions by the way???

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    10
    My Mood
    Nerdy
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Sort in Cyrilic order

    Give me an example of comparison operators.

  4. #4
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.

Similar Threads

  1. Fill & sort an array
    By yroll in forum Collections and Generics
    Replies: 2
    Last Post: April 1st, 2010, 06:00 AM
  2. can't figure out how to sort an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 6th, 2010, 03:07 PM
  3. Hibbard Shell Sort
    By biglandphil in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 12th, 2009, 10:14 PM
  4. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM
  5. Help with Sort arrays
    By drk in forum Collections and Generics
    Replies: 5
    Last Post: September 6th, 2009, 02:48 AM