字符串的Java集合排序方法不适用于区分大小写和特殊字符

时间:2019-03-18 07:16:05

标签: java sorting collections

我当时正在对Java(1.8)中的String列表进行排序,后来发现它不能按预期工作!

我正在尝试以下代码进行排序:

private Set<String> getTestData() {
    Set<String> compRoles = new HashSet<>();
    compRoles.add("AA");
    compRoles.add("Aa");
    compRoles.add("aA");
    compRoles.add("aa");
    compRoles.add("11");
    compRoles.add("117");
    compRoles.add("12");
    compRoles.add("21");
    compRoles.add("!@");
    compRoles.add("@!");
    compRoles.add("@@!");
    compRoles.add("BB");
    compRoles.add("Bb");
    compRoles.add("bb");
    return compRoles;
}

public static void main(String args[]) {
    List<String> test = new ArrayList<>(new Test().getTestData());
    System.out.println(test);
    Collections.sort(test);
    System.out.println(test);
}

排序前: [AA, Aa, aA, aa, 11, BB, Bb, bb, 12, @!, @@!, 117, 21, !@]

排序后: [!@, 11, 117, 12, 21, @!, @@!, AA, Aa, BB, Bb, aA, aa, bb]

我的期望是: [!@, @!, @@!, 11, 117, 12, 21, aa, aA, Aa, AA, bb, Bb, BB]

我是否需要使用其他自然排序方式?

2 个答案:

答案 0 :(得分:31)

您可以使用Java的Collator类。

public static void main(String[] args) {
    List<String> test = new ArrayList<>(new Test().getTestData());
    System.out.println(test);
    test.sort(Collator.getInstance(Locale.ENGLISH));
    System.out.println(test);
}

输出:-

[AA, Aa, aA, aa, 11, BB, Bb, bb, 12, @!, @@!, 117, 21, !@]
[!@, @!, @@!, 11, 117, 12, 21, aa, aA, Aa, AA, bb, Bb, BB]

答案 1 :(得分:3)

您可以为您的排序逻辑创建自定义comparator。之后,您可以像这样使用它:

ffmpeg_test.go:77: ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
      built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
      configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
      libavutil      54. 31.100 / 54. 31.100
      libavcodec     56. 60.100 / 56. 60.100
      libavformat    56. 40.101 / 56. 40.101
      libavdevice    56.  4.100 / 56.  4.100
      libavfilter     5. 40.101 /  5. 40.101
      libavresample   2.  1.  0 /  2.  1.  0
      libswscale      3.  1.101 /  3.  1.101
      libswresample   1.  2.101 /  1.  2.101
      libpostproc    53.  3.100 / 53.  3.100
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x17454c0] stream 0, offset 0x28: partial file
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/dev/stdin':
      Metadata:
        major_brand     : M4A 
        minor_version   : 512
        compatible_brands: isomiso2
        encoder         : Lavf56.40.101
      Duration: 00:00:05.02, bitrate: N/A
        Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 134 kb/s (default)
        Metadata:
          handler_name    : SoundHandler
    Output #0, wav, to 'pipe:':
      Metadata:
        major_brand     : M4A 
        minor_version   : 512
        compatible_brands: isomiso2
        ISFT            : Lavf56.40.101
        Stream #0:0(und): Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s (default)
        Metadata:
          handler_name    : SoundHandler
          encoder         : Lavc56.60.100 pcm_s16le
    Stream mapping:
      Stream #0:0 -> #0:0 (aac (native) -> pcm_s16le (native))
    [mov,mp4,m4a,3gp,3g2,mj2 @ 0x17454c0] stream 0, offset 0x28: partial file
    /dev/stdin: Invalid data found when processing input
    size=       0kB time=00:00:00.00 bitrate=N/A    
    video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
    Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)