Sorting String with non-western characters

前端 未结 6 1768
醉话见心
醉话见心 2020-12-15 01:49

I wanted to print sorted Polish names of all available languages.

import java.util.*;

public class Tmp
{
  public static void main(String... args)
  {
    L         


        
6条回答
  •  天涯浪人
    2020-12-15 02:04

    I'am dealing with the same problem. I found that the local collector solution works fine for android 7.0, but does not on earlier android versions. I've implemented the following algorithm. It is pretty fast ( I sort more than 3000 strings) and does it on earlier android versions too.

    public class SortBasedOnName implements Comparator {
    
        private Map myCharMap;
        private final static MapmyPolCharTable = new HashMap();
        static {
            myPolCharTable.put(' ',0x0020);
            myPolCharTable.put('!',0x0021);
            myPolCharTable.put('"',0x0022);
    
    
            myPolCharTable.put('a',0x0040);
            myPolCharTable.put('ą',0x0041);
            myPolCharTable.put('b',0x0042);
            myPolCharTable.put('c',0x0043);
            myPolCharTable.put('ć',0x0044);
    
    
            myPolCharTable.put('{',0x0066);
            myPolCharTable.put('|',0x0067);
            myPolCharTable.put('}',0x0068);
        }
    
        public SortBasedOnName() {}
    
        public int compare(Object o1, Object o2) {
    
            Dictionary dd1 = (Dictionary) o1;
            Dictionary dd2 = (Dictionary) o2;
    
        return strCompareWithDiacritics(dd1.getOriginal(), dd2.getOriginal());
        }
    
        private  int strCompareWithDiacritics(String s1, String s2) {
    
            int i = 0;
            int result = 0;
            int length =0;
    
            s1 = s1.toLowerCase();
            s2 = s2.toLowerCase();
            if (s1.length() > s2.length()) {
                result = 1;
                length = s2.length();
            } else if (s1.length() < s2.length()) {
                result = -1;
                length = s1.length();
            } else if (s1.length() == s2.length()) {
                result = 0;
                length = s1.length();
            }
    
            try {
                while (i  myPolCharTable.get(s2.charAt(i))) {
                        result = 1;
                        break;
                    } else if (myPolCharTable.get(s1.charAt(i)) < myPolCharTable.get(s2.charAt(i))) {
                        result = -1;
                        break;
                    }
                    i++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    }
    

提交回复
热议问题