简体   繁体   English

按以下条件对字符串的arraylist进行排序?

[英]Sort an arraylist of strings with following conditions?

I have an arraylist of strings in the form of: 我有一个字符串的arraylist形式:

{T1, C1, C2, T2, C3, T3, A1, T4, G1, A2, G2, A3, T5, C4, C5, T6, A4, T7, T8, C6, T9, A5, T10, G3, A6, A7, A8, C7, C8, T11, T12, C9, A9, $1, G4, A10, C10, C11, A11, A12, A13, A14, T13, T14, C12, T15, C13, C14, G5, G6, C15} {T1,C1,C2,T2,C3,T3,A1,T4,G1,A2,G2,A3,T5,C4,C5,T6,A4,T7,T8,C6,T9,A5,T10,G3,A6 ,A7,A8,C7,C8,T11,T12,C9,A9,$ 1,G4,A10,C10,C11,A11,A12,A13,A14,T13,T14,C12,T15,C13,C14,G5,G6 ,C15}

I want to sort this arraylist to the following: 我想将这个arraylist排序为以下内容:

{$1, A1, A2...A10, A11,... C1, C2...C14....}. {$ 1,A1,A2 ...... A10,A11,...... C1,C2 ...... C14 ....}。

however, when I used Collections.sort() , the result turns out to be: 但是,当我使用Collections.sort() ,结果结果是:

{$1, A1, A10, A11, A12, A13, A14, A2, A3, A4, A5, A6, A7, A8, A9, C1, C10, C11, C12, C13, C14, C15, C2, C3, C4, C5, C6, C7, C8, C9, G1, G2, G3, G4, G5, G6, T1, T10, T11, T12, T13, T14, T15, T2, T3, T4, T5, T6, T7, T8, T9} {$ 1,A1,A10,A11,A12,A13,A14,A2,A3,A4,A5,A6,A7,A8,A9,C1,C10,C11,C12,C13,C14,C15,C2,C3,C4 ,C5,C6,C7,C8,C9,G1,G2,G3,G4,G5,G6,T1,T10,T11,T12,T13,T14,T15,T2,T3,T4,T5,T6,T7,T8 ,T9}

with A2 comes after A14. A2之后是A14。 So is there a way to solve this issue? 有没有办法解决这个问题? Thanks so much! 非常感谢!

You can use Comparator.comparing() method chain to define the sort criteria. 您可以使用Comparator.comparing()方法链来定义排序条件。 Define the first comparison as Character sort on the first character and then define the second comparison as Integer sort on the remaining sub-string: 在第一个Character上将第一个比较定义为Character排序,然后在剩余的子字符串上将第二个比较定义为Integer排序:

String[] arr = {"T1", "C1", "C2", "T2", "C3", "T3", "A1", "T4", "G1", "A2", "G2", "A3", "T5",
    "C4", "C5", "T6", "A4", "T7", "T8", "C6", "T9", "A5", "T10", "G3", "A6", "A7", "A8", "C7",
    "C8", "T11", "T12", "C9", "A9", "$1", "G4", "A10", "C10", "C11", "A11", "A12", "A13",
    "A14", "T13", "T14", "C12", "T15", "C13", "C14", "G5", "G6", "C15"};
Arrays.sort(arr, Comparator.<String, Character>comparing(s -> s.charAt(0))
    .thenComparingInt(s -> Integer.parseInt(s.substring(1))));
System.out.println(Arrays.toString(arr));

will print 将打印

[$1, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, C12, C13, C14, C15, G1, G2, G3, G4, G5, G6, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15]

You must make an own Comparator object for sorting in another order than default. 您必须创建一个自己的Comparator对象,以便按照默认顺序排序。 Strings are sorted in lexicogrphic order when you do not supply an own comparator. 当您不提供自己的比较器时,字符串按字典顺序排序。 Instead of Collection.sort you can use the new method of List . 您可以使用List的新方法代替Collection.sort

myList.sort(new MyComparator());

The class MyComparator is an instance of Comparator : MyComparator类是Comparator一个实例:

class MyComparator implements Comparator<String> {
   ...
}

You have to implement the method compareTo . 你必须实现compareTo方法。

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

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