zoukankan      html  css  js  c++  java
  • Comparison 、IComparer

    一个是委托,一个是接口,

    // Summary:
    // Defines a method that a type implements to compare two objects.
    //
    // Type parameters:
    // T:
    // The type of objects to compare.This type parameter is contravariant. That
    // is, you can use either the type you specified or any type that is less derived.
    // For more information about covariance and contravariance, see Covariance
    // and Contravariance in Generics.
    public interface IComparer<in T>
    {
    // Summary:
    // Compares two objects and returns a value indicating whether one is less than,
    // equal to, or greater than the other.
    //
    // Parameters:
    // x:
    // The first object to compare.
    //
    // y:
    // The second object to compare.
    //
    // Returns:
    // A signed integer that indicates the relative values of x and y, as shown
    // in the following table.Value Meaning Less than zerox is less than y.Zerox
    // equals y.Greater than zerox is greater than y.
    int Compare(T x, T y);
    }

    定义如下:如果返回负数,表示x<y,如果返回正数,表示x>y,在排序中可以这么理解,

    x<y,返回负数,表示承认x比y小,这样就能排出升序的效果。

    deductionStrategyEntitiesTmp.Sort((x, y) =>
    {
    if (x.StartAmount > y.StartAmount) return -1;
    else if (x.StartAmount < y.StartAmount) return 1;
    else return 0;
    }); //扣减金额递减

      //分数由大变小

    Collections.sort(promotionActivitiesResult, new Comparator<PromotionActivity>() {
    @Override
    public int compare(PromotionActivity o1, PromotionActivity o2) {
    return o2.getSortScore() > o1.getSortScore() ? 1 : o2.getSortScore() < o1.getSortScore() ? -1 : 0;
    }
    });
  • 相关阅读:
    ios web 媒体查询兼容
    Linux python 虚拟环境管理
    three.js 纹理动画实现
    three.js 在模型上移动相机
    three.js 模型拖动之DragControls控制器
    three.js 添加html内容、文本
    微信公众号对接记录
    事务的日志
    事务的隔离级别
    事务中的锁
  • 原文地址:https://www.cnblogs.com/wuMing-dj/p/5416981.html
Copyright © 2011-2022 走看看