Sorting String with non-western characters

前端 未结 6 1758
醉话见心
醉话见心 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 01:54

    Something like this

    val polishCollator = yourCollection.sortedWith(Comparator { s1, s2 ->
                Collator.getInstance(Locale("pl", "PL")).compare(s1,s2)
            })
    
    0 讨论(0)
  • 2020-12-15 01:58

    try

    Collections.sort(langs, Collator.getInstance(new Locale("pl", "PL")));
    

    it will produce

    ...
    litewski
    łotewski
    ...
    

    see Collator API for details

    0 讨论(0)
  • 2020-12-15 02:00

    Have a look at java.text.Collator.newInstance(Locale). You need to supply the Polish locale in your case. Collators implement the Comparator interface, so you can use that in sort APIs and in sorted datastructures like TreeSet.

    0 讨论(0)
  • 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<Character, Integer> myCharMap;
        private final static Map<Character, Integer>myPolCharTable = new HashMap<Character, Integer>();
        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 <length) {
                    if (myPolCharTable.get(s1.charAt(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;
        }
    }
    
    0 讨论(0)
  • 2020-12-15 02:12

    Unfortunately in Polish ł comes after l and before m so the output should be:

    You can define your own Compararable or Comparator interface.

    Or also this might help you:

    • Sort List of Strings with Localization
    0 讨论(0)
  • 2020-12-15 02:19

    You should pass a Collator to the sort method:

    // sort according to default locale
    Collections.sort(langs, Collator.getInstance());
    

    The default sort order is defined by the Unicode codepoints in the string, and that's not the correct alphabetical order in any language.

    0 讨论(0)
提交回复
热议问题