sorted 多条件排序

参考:

1、https://www.cnblogs.com/bymo/p/8335502.html

2、https://blog.csdn.net/qq_36575363/article/details/108930409

 

n=int(input())
dic={}
for index in  range(n):
        ls=input()
        if 'append'in ls:
                temp=[int(i) for i in ls.split()[1:]]
                x,y=temp[0],temp[1]
                if x in dic:
                        dic[x]=dic[x]+y
                else:
                        dic[x]=y
        if ls=='query':
                if len(dic)<=10 and len(dic)>0:
                        s = sorted(dic.items(), key=lambda x:(-x[1], x[0]))
                        print([x[0] for x in s])
                elif len(dic)>10:
                        s = sorted(dic.items(), key=lambda x:(-x[1], x[0]))[:10]
                        print([x[0] for x in s])
                else:
                        print('null')

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: stream.sorted可以使用Comparator来实现多条件排序。例如,我们可以使用以下代码对一个Person对象列表进行排序,首先按照年龄升序排序,如果年龄相同则按照姓名字典序排序: ``` List<Person> people = ...; List<Person> sortedPeople = people.stream() .sorted(Comparator.comparingInt(Person::getAge) .thenComparing(Person::getName)) .collect(Collectors.toList()); ``` 其中,Comparator.comparingInt(Person::getAge)表示按照年龄升序排序,thenComparing(Person::getName)表示在年龄相同的情况下按照姓名字典序排序。 ### 回答2: Java中的stream.sorted()方法可以用于对流中的元素进行排序。通常情况下,我们直接使用 sorted() 时只考虑其中一个排序条件是否满足,但实际上,我们可能需要一次对多个排序条件进行排序。对此,Java中提供了很好的解决方案。 在Java中,当我们需要对对象的多个值进行排序时,我们可以使用 Comparator.comparing() 函数。Comparator.comparing() 接受一个字符串类型的属性名称作为参数,并生成一个比较器用于比较该属性。通过多次嵌套调用 Comparator.comparing() ,我们可以轻松地实现多个条件排序。例如: ```Java List<Person> people = // ... some list of Person objects List<Person> sortedPeople = people.stream() .sorted(Comparator.comparing(Person::getLastName) .thenComparing(Person::getFirstName)) .collect(Collectors.toList()); ``` 上面的代码中,我们首先按照 Person 对象的 lastName 属性进行排序,并在 lastName 属性相同时再按照 firstName 属性排序。在多条件排序的过程中,我们通过使用 thenComparing() 方法链接多个比较器来实现排序。如果 lastName 在两个 Person 对象中相同,则会使用 firstName 属性进行排序。 除了使用 Comparator.comparing() 方法之外,我们还可以使用更高阶的函数,例如 Comparator.thenComparing() 。这个方法接受一个 lambda 表达式,该表达式以当前对象为参数,返回一个可与另一个对象进行比较的“按值”比较。例如,如果我们想要按照 int 类型的属性 a 和 b 进行排序,我们可以使用下列代码: ```Java Comparator<MyObject> comparator = Comparator.comparing(MyObject::getA) .thenComparingInt(MyObject::getB); ``` 通过使用上述代码,我们可以使用 Java 中的 stream.sorted() 方法来轻松实现多条件排序。无论我们需要按照多少个元素排序,只需通过嵌套多个比较器即可实现。 ### 回答3: 在Java 8中,通过流对象可以使用.sorted()方法根据单一条件对元素进行排序。例如,对一组数字进行排序,可以使用以下代码: Stream<Integer> numbers = Stream.of(1,5,3,2,4); List<Integer> sortedNumbers = numbers.sorted().collect(Collectors.toList()); 这段代码将数字1,5,3,2,4排成一个有序列表。然而,在实际使用中,我们往往需要按照多个条件进行排序。Java 8中,我们可以调用Stream.sorted()方法并传递一个Comparator实例作为参数,以实现按照多个条件进行排序。 Comparator接口是用于定义排序规则的接口。我们可以根据自己的需要来实现Comparator接口,并在其中指定多个排序条件。下面是一个简单的例子: List<Person> people = new ArrayList<>(); people.add(new Person("Alice", 25)); people.add(new Person("Bob", 30)); people.add(new Person("Charlie", 20)); people.add(new Person("David", 35)); List<Person> sortedPeople = people .stream() .sorted(Comparator .comparing(Person::getAge) .thenComparing(Person::getName)) .collect(Collectors.toList()); 在这个例子中,我们定义了一个Person类,其中包含名字和年龄两个字段。我们使用stream()方法将人员列表转换为流对象,并在.sorted()方法中传递一个Comparator实例。该实例使用两个排序条件:首先以年龄从小到大进行排序,如果年龄相同,则按照名字从小到大排序。最后,使用.collect(Collectors.toList())方法将排序后的结果转换为列表对象。 需要注意的是,对于排序的规则,我们可以通过调用Comparator.comparing()方法配合lambda表达式来实现。在上面的例子中,我们使用Person::getAge和Person::getName来作为比较的关键字。 总之,通过使用Stream.sorted()方法,并结合自定义的Comparator实现,我们可以方便地在Java 8中实现按照多个条件进行排序。对于复杂的排序需求,这是一种非常有用的技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值