看到一段代码:
不是很理解
1 2 3 4 5 6 7 | materialVOList?.sort { a, b -> if (a.count == b.count) { return a.name <=> b.name } b.count <=> a.count } |
然后自己写了哥demo测试了一下具体是怎么实现得。
doMain 类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Student { Integer id String name Integer count @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", count=" + count + '}'; } } |
测试类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public static void main(String[] args) { List<Student> list = Lists.newArrayList() Student student1 = new Student(); student1.name = "a" student1.id = 1 student1.count = 1 Student student2 = new Student(); student2.name = "b" student2.id = 2 student2.count = 2 Student student3 = new Student(); student3.name = "d" student3.id = 3 student3.count = 3 Student student4 = new Student(); student4.name = "c" student4.id = 4 student4.count = 3 Student student5 = new Student(); student5.name = "e" student5.id = 5 student5.count = 5 list.add(student1) list.add(student2) list.add(student3) list.add(student4) list.add(student5) println(list) list?.sort { a, b -> if (a.count == b.count) { return a.name <=> b.name } b.count <=> a.count } println(list) |
我把id=3 student name =“d” id=4 得student name=“c”
排序为:id 4 在 id 3 得前面;如果互相换得话则相反;
看下源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | /** * Compares two strings lexicographically. * The comparison is based on the Unicode value of each character in * the strings. The character sequence represented by this * {@code String} object is compared lexicographically to the * character sequence represented by the argument string. The result is * a negative integer if this {@code String} object * lexicographically precedes the argument string. The result is a * positive integer if this {@code String} object lexicographically * follows the argument string. The result is zero if the strings * are equal; {@code compareTo} returns {@code 0} exactly when * the {@link #equals(Object)} method would return {@code true}. * <p> * This is the definition of lexicographic ordering. If two strings are * different, then either they have different characters at some index * that is a valid index for both strings, or their lengths are different, * or both. If they have different characters at one or more index * positions, let <i>k</i> be the smallest such index; then the string * whose character at position <i>k</i> has the smaller value, as * determined by using the < operator, lexicographically precedes the * other string. In this case, {@code compareTo} returns the * difference of the two character values at position {@code k} in * the two string -- that is, the value: * <blockquote><pre> * this.charAt(k)-anotherString.charAt(k) * </pre><p><center>[wp_ad_camp_3]</center></p></blockquote> * If there is no index position at which they differ, then the shorter * string lexicographically precedes the longer string. In this case, * {@code compareTo} returns the difference of the lengths of the * strings -- that is, the value: * <blockquote><pre> * this.length()-anotherString.length() * </pre></blockquote> * * @param anotherString the {@code String} to be compared. * @return the value {@code 0} if the argument string is equal to * this string; a value less than {@code 0} if this string * is lexicographically less than the string argument; and a * value greater than {@code 0} if this string is * lexicographically greater than the string argument. */ public int compareTo(String anotherString) { int len1 = value.length; int len2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value; int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; } |
看到这里就很清楚了:就是说如果count相同 比name 的Unicode。这样就解决啦!