list集合中的sort排序compareTo()groovy

看到一段代码:
不是很理解

  materialVOList?.sort {
            a, b ->
                if (a.count == b.count) {
                    return a.name <=> b.name
                }
                b.count <=> a.count
        }

然后自己写了哥demo测试了一下具体是怎么实现得。
doMain 类

class Student {

    Integer id

    String name

    Integer count


    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", count=" + count +
                '}';
    }
}

测试类:

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 得前面;如果互相换得话则相反;
看下源码:

 /**
     * 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 &lt; 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></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。这样就解决啦!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值