我有一个字符串规则:
String MACEDONIAN_ALPH = "А < Б < В < Г < Д < Ѓ < Е < Ж < З < S < И < Ј < К < Л < Љ < М < Н < Њ < О < П < Р < С < Т < Ќ < У < Ф < Х < Ц < Ч < Џ < Ш";
然后我创建RuleBasedCollator
:
try {
macedonian_collator = new RuleBasedCollator(MACEDONIAN_ALPH);
} catch (ParseException e) {
e.printStackTrace();
}
当我呼叫Collections.sort(arrayList, macedonian_collator);
时,它不会从预定义的规则中排序。
它排序arrayList
从Ѓ, Ј
等提升,然后排序好
编辑:这里添加了最小的例子
ArrayList<String> citiesList = new ArrayList<>();
citiesList.add("ЈАНЕ САНДАНСКИ");
citiesList.add("СКОПЈЕ");
citiesList.add("БЕЛГРАД");
citiesList.add("ЃОРЧЕ ПЕТРОВ");
Log.v("test123", "printing before sorting");
for (int i = 0; i < 4; i++)
{
Log.v("test123", citiesList.get(i));
}
try {
macedonian_collator = new RuleBasedCollator(MACEDONIAN_ALPH);
} catch (ParseException e) {
e.printStackTrace();
}
Collections.sort(citiesList, macedonian_collator);
Log.v("test123", "printing after sorting");
for (int i = 0; i < 4; i++)
{
Log.v("test123", citiesList.get(i));
}
这里是完整的logcat:
12-31 16:53:52.772 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: printing before sorting
12-31 16:53:52.772 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: ЈАНЕ САНДАНСКИ
12-31 16:53:52.772 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: СКОПЈЕ
12-31 16:53:52.772 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: БЕЛГРАД
12-31 16:53:52.772 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: ЃОРЧЕ ПЕТРОВ
12-31 16:53:52.773 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: java.text.ParseException: expected a reset or setting or comment at index 0 near "!< А < Б < В < Г"
12-31 16:53:52.773 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.icu.impl.coll.CollationRuleParser.makeParseException(CollationRuleParser.java:853)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.icu.impl.coll.CollationRuleParser.setParseError(CollationRuleParser.java:843)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.icu.impl.coll.CollationRuleParser.parse(CollationRuleParser.java:162)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.icu.impl.coll.CollationRuleParser.parse(CollationRuleParser.java:114)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.icu.impl.coll.CollationBuilder.parseAndBuild(CollationBuilder.java:76)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.icu.text.RuleBasedCollator.internalBuildTailoring(RuleBasedCollator.java:228)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.icu.text.RuleBasedCollator.<init>(RuleBasedCollator.java:208)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at java.text.RuleBasedCollator.<init>(RuleBasedCollator.java:294)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at com.mojavozcka.vozenredmakedonskizeleznici.PocetnaActivity$override.onCreate(PocetnaActivity.java:46)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at com.mojavozcka.vozenredmakedonskizeleznici.PocetnaActivity$override.access$dispatch(PocetnaActivity.java)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at com.mojavozcka.vozenredmakedonskizeleznici.PocetnaActivity.onCreate(PocetnaActivity.java:0)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.Activity.performCreate(Activity.java:6679)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4519)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.ActivityThread.-wrap19(ActivityThread.java)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1483)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.os.Looper.loop(Looper.java:154)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at java.lang.reflect.Method.invoke(Native Method)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
12-31 16:53:52.774 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
12-31 16:53:52.775 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: printing after sorting
12-31 16:53:52.775 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: ЃОРЧЕ ПЕТРОВ
12-31 16:53:52.775 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: ЈАНЕ САНДАНСКИ
12-31 16:53:52.775 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: БЕЛГРАД
12-31 16:53:52.775 13271-13271/com.mojavozcka.vozenredmakedonskizeleznici V/test123: СКОПЈЕ
P.S。我在Android Studio for Android app
工作我尝试使用像String MACEDONIAN_ALPH_UNICODE = "\u0410 < \u0411 < \u0412 < \u0413 < \u4014 < \u0403 < \u0415 < \u0416 < \u0417 < \u0418 < \u0408 \u041a < \u041b < \u0409 < \u041c < \u041d < \u040a <\u041e < \u041f < \u0420 < \u0421 < \u0422 < \u0420 < \u0423 < \u0424 < \u0425 < \u0426 < \u0427 < \u040f < \u0428";