python cmp_to_key

import functools

x = [1, 3, 2, 4, 5]


def cmp_rise(a, b):
    '''
    升序排序:
    当前面的参数a小于后面的参数b返回-1,-1代表交换顺序,
    当前面的的参数a大于等于后面的参数b返回1,1代表保持不变。
    因此保证了前面的数子小于后面的数字,是升序排序。
    '''
    if a < b:
        return -1
    else:
        return 1


def cmp_decline(a, b):
    '''
    降序排序:
    当前面的参数a小于后面的参数b返回1,1代表保持不变,
    当前面的的参数a大于等于后面的参数b返回-1,-1代表交换顺序。
    因此保证了前面的数子大于后面的数字,是降序排序。
    '''
    if a < b:
        return 1
    else:
        return -1


x_sorted_by_rise = sorted(x, key=functools.cmp_to_key(cmp_rise))
x_sorted_by_decline = sorted(x, key=functools.cmp_to_key(cmp_decline))
print(x)  # 输出结果:[1, 3, 2, 4, 5]
print(x_sorted_by_rise)  # 输出结果:[1, 2, 3, 4, 5]
print(x_sorted_by_decline)  # 输出结果:[5, 4, 3, 2, 1]

注意

  • a代表后面的数字,b代表前面的数字。
    在这里插入图片描述
    Java同理

返回1: 不交换, 返回-1表示交换


import java.util.*;

//该类实现了两种接口
public class Dog implements Comparator<Dog>, Comparable<Dog> {
    //类里有两个成员变量
    private String name;
    private int age;

    public Dog() {
    }

    public Dog(String n, int a) {
        name = n;
        age = a;
    }

    public String getDogName() {
        return name;
    }

    public int getDogAge() {
        return age;
    }

    // Overriding the compareTo method
//自然排序定义为比较name这个字符串
    public int compareTo(Dog d) {
        return (this.name).compareTo(d.name);//这里使用了String类的compareTo方法
    }

    // Overriding the compare method to sort the age
//定义一个比较器来比较年龄.如果想让年龄逆序,则返回d1.age - d.age.
    public int compare(Dog d, Dog d1) {
        if (d.age - d1.age > 0){
            return -1;
        }else{
            return 1;
        }
    }
}

降序排列

import com.leetcode.Dog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Example {

    public static void main(String args[]) {
        // Takes a list o Dog objects
        List<
                Dog>
                list = new ArrayList<Dog>();

        list.add(new Dog("Shaggy", 3));
        list.add(new Dog("Lacy", 2));
        list.add(new Dog("Roger", 10));
        list.add(new Dog("Tommy", 4));
        list.add(new Dog("Tammy", 1));
        //使用Comparable接口的compareTo方法自然排序,排序的是字符串
//        Collections.sort(list);   // Sorts the array list
//
//        for (Dog a : list)   // printing the sorted list of names
//            System.out.print(a.getDogName() + ", ");

        // Sorts the array list using comparator
        //sort()的第二个形参是个带有比较器的对象,因此使用该对象中compare()方法
        Collections.sort(list, new Dog());
        System.out.println(" ");

        for (Dog a : list)   // printing the sorted list of ages
            System.out.print(a.getDogName() + "  : " + a.getDogAge() + ", ");
    }
}
// 输出结果:
//Roger  : 10, Tommy  : 4, Shaggy  : 3, Lacy  : 2, Tammy  : 1, 
//Process finished with exit code 0

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值