简体   繁体   English

如何按包含非英文字符的字段按字母顺序对 ArrayList 进行排序? Java

[英]How to sort ArrayList alphabetically by fields that contain non-english characters? Java

I have an ArrayList of custom objects that I want to sort alphabetically.我有一个自定义对象的 ArrayList,我想按字母顺序排序。 The problem is that the field I want to sort it by sometimes contains non-english characters, like á or é .问题是我想要对其进行排序的字段有时包含非英文字符,例如áé I wanted to do this by using Collections.sort() , but with this method, the items with non-english strings in their fields don't get sorted correctly.我想通过使用Collections.sort()来做到这一点,但是使用这种方法,字段中包含非英语字符串的项目无法正确排序。

This is what I've tried:这是我尝试过的:

public List<Video> sortDatabase(List<Video> videos){
        List<Video> sortedList = new ArrayList<>(videos);
        Collections.sort(sortedList, new Comparator<Video>() {
            @Override
            public int compare(Video video, Video t1) {
                return video.getTitle().compareTo(t1.getTitle());
            }
        });
        return orderedList;
    }

EDIT编辑
I am using Android Studio with minSdkVersion 21我正在使用带有minSdkVersion 21的 Android Studio

I've found a good answer here .我在这里找到了一个很好的答案。 This is how it looks like in my case:这就是我的情况:

public List<Video> sortDatabase(List<Video> videos){
        List<Video> sortedList = new ArrayList<>(videos);
        orderedList.sort(new VideoComparator());
        return orderedList;
    }

private static class VideoComparator implements Comparator<Video>{
            Collator spCollator = Collator.getInstance(new Locale("hu", "HU"));
            public int compare (Video e1, Video e2){
                return spCollator.compare(e1.getTitle(), e2.getTitle());
            }
        }

In my case, the Locale had to be constructed with ("hu", "HU") .就我而言,必须使用("hu", "HU")构建语言环境。 You can check all supported languages here您可以在此处查看所有支持的语言

This solution works fine, however, I am trying to use this for an Android application where this method only works with API level 24 or higher.此解决方案工作正常,但是,我尝试将其用于 Android 应用程序,其中此方法仅适用于 API 24 级或更高级别。 So if anybody has a different solution, I will make that the accepted answer.因此,如果有人有不同的解决方案,我将把它作为公认的答案。

but with this method, the items with non-english (sic) strings in their fields don't get sorted correctly.但是使用这种方法,字段中包含非英语(原文如此)字符串的项目无法正确排序。

Are you sure that's the problem?你确定这是问题吗? Could it be that maybe you do not understand what is involved in sorting?会不会是你不明白排序涉及什么? I am pretty sure that the problem is the Unicode values of the characters.我很确定问题是字符的 Unicode 值。 Characters are organized in character sets.字符按字符集组织。 The entire English language characters have a Unicode value lower or higher than characters from other languages, thus giving the impression that the sorting went wrong.整个英文字符的 Unicode 值低于或高于其他语言字符,从而给人以排序错误的印象。

Maybe you should take a look at the Unicode chart to fully understand what I am saying.也许你应该看看Unicode 图表来完全理解我在说什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM