reactive-programming - Sorted order in Flux of custom class
问题描述
Suppose I have a class Student with attributes name and height.
class Student{ String name; double height;}
If I have a flux of objects of students and I want to the output to be sorted in ascending order of the student's name, then how do I do that?
解决方案
假设您有一个学生对象数组,如下所示:
Student[] students = {studentObj1, studentObj2, studentObj3};
您只需要在 Flux 提供的sort方法中使用 Comparator,它在 Java 8/反应式编程中可以写成 lambda 函数。
这里 obj1 和 obj2 是相互比较的 Student 类的对象。
obj1.compareTo(obj2) 按升序对它们进行排序。
Flux.fromIterable (Arrays.asList(students)).sort( (obj1, obj2) -> obj1.getName().compareTo(obj2.getName()));
推荐阅读
- excel - 将工作表复制到两个目录上
- javascript - 如何将可拖动元素限制为正文、禁用文本选择和启用表单输入?
- scala - 如何创建通用方法来解析嵌套的 json?
- node.js - 如何更新MongoDB上其他数组中的数组值?
- scala - 使用 Scala 为 spark 编写 Parquet 文件,而不需要 spark 作为依赖项
- xamarin - 如何更改 ListView 选定项的背景颜色?
- c# - 不可调用的成员“System.Web.UI.Page.ClientScript”不能像方法一样使用
- php - 简单的 OOP 数组参数不返回值,而是说为 foreach() 提供的参数无效
- matlab - Matlab条形图中的XTicks未与其条形对齐
- android - 带 web 套接字的推送服务器