Java 如何按某些属性对对象列表进行排序
我有简单的课程Java 如何按某些属性对对象列表进行排序,java,sorting,sort-object,Java,Sorting,Sort Object,我有简单的课程 public class ActiveAlarm { public long timeStarted; public long timeEnded; private String name = ""; private String description = ""; private String event; private boolean live = false; } 和列表con。如何按时间开始,然后按时间结束升序排序?有人
public class ActiveAlarm {
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
}
和列表
con。如何按时间开始
,然后按时间结束
升序排序?有人能帮忙吗?在C++中,用泛型算法和过载算子使用
例如:class Score {
private String name;
private List<Integer> scores;
// +accessor methods
}
class Student{
private String name;
private List<Score> scores;
// +accessor methods
}
class Score {
private int grade;
// +accessor methods
}
使用
例如:
class Score {
private String name;
private List<Integer> scores;
// +accessor methods
}
class Student{
private String name;
private List<Score> scores;
// +accessor methods
}
class Score {
private int grade;
// +accessor methods
}
您可以调用Collections.sort()并传入一个比较器,您需要编写该比较器来比较对象的不同属性。您可以调用Collections.sort()并传入一个比较器,您需要编写该比较器来比较对象的不同属性。如果存在单个“自然”排序顺序,则可以使
ActiveAlarm
implement具有可比性。。。否则(如果您碰巧想要按照特定的顺序排序,但可能同样很容易需要不同的顺序),最好实现。老实说,这种特殊情况可能会有任何结果。。。但我可能会坚持更灵活的选择
编辑:示例实现:
public class AlarmByTimesComparer implements Comparator<ActiveAlarm> {
@Override
public int compare(ActiveAlarm x, ActiveAlarm y) {
// TODO: Handle null x or y values
int startComparison = compare(x.timeStarted, y.timeStarted);
return startComparison != 0 ? startComparison
: compare(x.timeEnded, y.timeEnded);
}
// I don't know why this isn't in Long...
private static int compare(long a, long b) {
return a < b ? -1
: a > b ? 1
: 0;
}
}
公共类AlarmByTimeComparer实现比较器{
@凌驾
公共整数比较(ActiveAlarm x、ActiveAlarm y){
//TODO:处理空的x或y值
int startComparison=比较(x.timeStarted,y.timeStarted);
返回开始比较!=0?开始比较
:比较(x.timeend,y.timeend);
}
//我不知道为什么这不会持续很久。。。
专用静态int比较(长a、长b){
返回ab?1
: 0;
}
}
如果存在单一的“自然”排序顺序,则可以使活动报警
实现可比较。。。否则(如果您碰巧想要按照特定的顺序排序,但可能同样很容易需要不同的顺序),最好实现。老实说,这种特殊情况可能会有任何结果。。。但我可能会坚持更灵活的选择
编辑:示例实现:
public class AlarmByTimesComparer implements Comparator<ActiveAlarm> {
@Override
public int compare(ActiveAlarm x, ActiveAlarm y) {
// TODO: Handle null x or y values
int startComparison = compare(x.timeStarted, y.timeStarted);
return startComparison != 0 ? startComparison
: compare(x.timeEnded, y.timeEnded);
}
// I don't know why this isn't in Long...
private static int compare(long a, long b) {
return a < b ? -1
: a > b ? 1
: 0;
}
}
公共类AlarmByTimeComparer实现比较器{
@凌驾
公共整数比较(ActiveAlarm x、ActiveAlarm y){
//TODO:处理空的x或y值
int startComparison=比较(x.timeStarted,y.timeStarted);
返回开始比较!=0?开始比较
:比较(x.timeend,y.timeend);
}
//我不知道为什么这不会持续很久。。。
专用静态int比较(长a、长b){
返回ab?1
: 0;
}
}
你可以使用和传递你自己的你可以使用和传递你自己的番石榴:
Collections.sort(列表,新比较器(){
@凌驾
公共整数比较(主动报警a1、主动报警a2){
返回ComparisonChain.start()
.比较(a1.时间开始,a2.时间开始)
//...
.比较(a1.TimeEnd,a1.TimeEnd).result();
}});
番石榴:
Collections.sort(列表,新比较器(){
@凌驾
公共整数比较(主动报警a1、主动报警a2){
返回ComparisonChain.start()
.比较(a1.时间开始,a2.时间开始)
//...
.比较(a1.TimeEnd,a1.TimeEnd).result();
}});
在java中,您需要使用staticCollections.sort
方法。下面是一个CompanyRole对象列表的示例,先按begin排序,然后按end排序。您可以轻松地适应自己的对象
private static void order(List<TextComponent> roles) {
Collections.sort(roles, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int x1 = ((CompanyRole) o1).getBegin();
int x2 = ((CompanyRole) o2).getBegin();
if (x1 != x2) {
return x1 - x2;
} else {
int y1 = ((CompanyRole) o1).getEnd();
int y2 = ((CompanyRole) o2).getEnd();
return y2 - y1;
}
}
});
}
私有静态无效订单(列出角色){
Collections.sort(角色,新的Comparator(){
@凌驾
公共整数比较(对象o1、对象o2){
intx1=((公司)o1).getBegin();
intx2=((公司)o2).getBegin();
如果(x1!=x2){
返回x1-x2;
}否则{
inty1=((CompanyRole)o1.getEnd();
inty2=((公司)o2).getEnd();
返回y2-y1;
}
}
});
}
在java中,您需要使用staticCollections.sort
方法。下面是一个CompanyRole对象列表的示例,先按begin排序,然后按end排序。您可以轻松地适应自己的对象
private static void order(List<TextComponent> roles) {
Collections.sort(roles, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int x1 = ((CompanyRole) o1).getBegin();
int x2 = ((CompanyRole) o2).getBegin();
if (x1 != x2) {
return x1 - x2;
} else {
int y1 = ((CompanyRole) o1).getEnd();
int y2 = ((CompanyRole) o2).getEnd();
return y2 - y1;
}
}
});
}
私有静态无效订单(列出角色){
Collections.sort(角色,新的Comparator(){
@凌驾
公共整数比较(对象o1、对象o2){
intx1=((公司)o1).getBegin();
intx2=((公司)o2).getBegin();
如果(x1!=x2){
返回x1-x2;
}否则{
inty1=((CompanyRole)o1.getEnd();
inty2=((公司)o2).getEnd();
返回y2-y1;
}
}
});
}
公共类ActiveAlarm{
公共服务长时间启动;
公众长期关注;
私有字符串名称=”;
私有字符串描述=”;
私有字符串事件;
private boolean live=false;
公共int比较(主动报警a){
如果(this.timeStarted>a.timeStarted)
返回1;
else if(this.timeStarteda.timeend)
返回1;
其他的
返回-1;
}
}
这应该给您一个大致的想法。完成后,您可以调用列表中的Collections.sort()
。公共类ActiveAlarm{
公共服务长时间启动;
公众长期关注;
私有字符串名称=”;
私有字符串描述=”;
私有字符串事件;
private boolean live=false;
公共int比较(主动报警a){
如果(this.timeStarted>a.timeStarted)
返回1;
else if(this.timeStartedpublic class ActiveAlarm implements Comparable<ActiveAlarm> {
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
public int compareTo(ActiveAlarm a) {
if ( this.timeStarted > a.timeStarted )
return 1;
else if ( this.timeStarted < a.timeStarted )
return -1;
else {
if ( this.timeEnded > a.timeEnded )
return 1;
else
return -1;
}
}
Collections.sort(arrayList, new Comparator<ActiveAlarm>() {
public int compare(ActiveAlarm o1, ActiveAlarm o2) {
//Sorts by 'TimeStarted' property
return o1.getTimeStarted()<o2.getTimeStarted()?-1:o1.getTimeStarted()>o2.getTimeStarted()?1:doSecodaryOrderSort(o1,o2);
}
//If 'TimeStarted' property is equal sorts by 'TimeEnded' property
public int doSecodaryOrderSort(ActiveAlarm o1,ActiveAlarm o2) {
return o1.getTimeEnded()<o2.getTimeEnded()?-1:o1.getTimeEnded()>o2.getTimeEnded()?1:0;
}
});
class ActiveAlarm implements Comparable<ActiveAlarm>{
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
public ActiveAlarm(long timeStarted,long timeEnded) {
this.timeStarted=timeStarted;
this.timeEnded=timeEnded;
}
public long getTimeStarted() {
return timeStarted;
}
public long getTimeEnded() {
return timeEnded;
}
public int compareTo(ActiveAlarm o) {
return timeStarted<o.getTimeStarted()?-1:timeStarted>o.getTimeStarted()?1:doSecodaryOrderSort(o);
}
public int doSecodaryOrderSort(ActiveAlarm o) {
return timeEnded<o.getTimeEnded()?-1:timeEnded>o.getTimeEnded()?1:0;
}
Collections.sort(list);
Collections.sort(list, (ActiveAlarm a1, ActiveAlarm a2) -> a1.timeStarted-a2.timeStarted);
Collections.sort(list, Comparator.comparingInt(ActiveAlarm ::getterMethod));
Collections.sort(list, new Comparator<ActiveAlarm>() {
@Override
public int compare(ActiveAlarm a1, ActiveAlarm a2) {
return a1.timeStarted - a2.timeStarted;
}
});
Collections.sort(list, (ActiveAlarm a1, ActiveAlarm a2) -> a1.timeStarted-a2.timeStarted
.thenComparing ((ActiveAlarm a1, ActiveAlarm a2) -> a1.timeEnded-a2.timeEnded)
);
Collections.sort(list, (ActiveAlarm a1, ActiveAlarm a2) -> a1.name.compareTo(a2.name) );
class Student{
private String name;
private List<Score> scores;
// +accessor methods
}
class Score {
private int grade;
// +accessor methods
}
Collections.sort(student.getScores(), Comparator.comparing(Score::getGrade);
ListOfActiveAlarmObj.sort((a,b->a.getTimeStarted().compareTo(b.getTimeStarted())))
ListOfActiveAlarmObj.sort(Comparator.comparing(ActiveAlarm::getTimeStarted))
List<ActiveAlarm> sorted =
list.stream()
.sorted(Comparator.comparingLong((ActiveAlarm alarm) -> alarm.timeStarted)
.thenComparingLong((ActiveAlarm alarm) -> alarm.timeEnded))
.collect(Collectors.toList());
List<ActiveAlarm> sorted =
list.stream()
.sorted(Comparator.comparingLong(ActiveAlarm::getTimeStarted)
.thenComparingLong(ActiveAlarm::getTimeEnded))
.collect(Collectors.toList());
list.sort(Comparator.comparingLong((ActiveAlarm alarm) -> alarm.timeStarted)
.thenComparingLong((ActiveAlarm alarm) -> alarm.timeEnded));
list.sort(Comparator.comparingLong(ActiveAlarm::getTimeStarted)
.thenComparingLong(ActiveAlarm::getTimeEnded));
class SortTest{
public static void main(String[] args) {
ArrayList<ActiveAlarm> activeAlarms = new ArrayList<>(){{
add(new ActiveAlarm("Alarm 1", 5, 10));
add(new ActiveAlarm("Alarm 2", 2, 12));
add(new ActiveAlarm("Alarm 3", 0, 8));
}};
/* I sort the arraylist here using the getter methods */
activeAlarms.sort(Comparator.comparing(ActiveAlarm::getTimeStarted)
.thenComparing(ActiveAlarm::getTimeEnded));
System.out.println(activeAlarms);
}
}
public class ActiveAlarm {
public long timeStarted;
public long timeEnded;
private String name = "";
private String description = "";
private String event;
private boolean live = false;
public ActiveAlarm(String name, long timeStarted, long timeEnded) {
this.name = name;
this.timeStarted = timeStarted;
this.timeEnded = timeEnded;
}
public long getTimeStarted() {
return timeStarted;
}
public long getTimeEnded() {
return timeEnded;
}
@Override
public String toString() {
return name;
}
}
[Alarm 3, Alarm 2, Alarm 1]
package in.ac.adit.oop.sort;
public class Employee {
private int id;
private String name;
private String department;
public int getId() {
return id;
}
public Employee() {
super();
}
public Employee(int id, String name, String department) {
super();
this.id = id;
this.name = name;
this.department = department;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]";
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}
package in.ac.adit.oop.sort;
import java.util.ArrayList;
import java.util.List;
public class Example {
public static void main(String[] args) {
/*
* Create 10 Employee Object
*/
Employee emp1 = new Employee(1, "Nayan", "IT");
Employee emp2 = new Employee(2, "Siddarth", "CP");
Employee emp3 = new Employee(3, "Samarth", "AE");
Employee emp4 = new Employee(4, "Bhavesh", "CV");
Employee emp5 = new Employee(5, "Sam", "FT");
Employee emp6 = new Employee(6, "Keyur", "IT");
Employee emp7 = new Employee(7, "Bala", "ME");
Employee emp8 = new Employee(8, "Mitul", "ME");
Employee emp9 = new Employee(9, "Kamlesh", "EE");
Employee emp10 = new Employee(10, "Piyush", "EE");
/*
* List of Employee Object
*/
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
employeeList.add(emp5);
employeeList.add(emp6);
employeeList.add(emp7);
employeeList.add(emp8);
employeeList.add(emp9);
employeeList.add(emp10);
CustomObjectSort customObjectSort = new CustomObjectSort();
List<Employee> sortByDepartment = customObjectSort.sortByDepartment(employeeList);
/*
* Sorted By Department
*/
for (Employee employee : sortByDepartment) {
System.out.println(employee);
}
/*
* Sorted By Name
*/
List<Employee> sortByName = customObjectSort.sortByName(employeeList);
for (Employee employee : sortByName) {
System.out.println(employee);
}
/*
* Sorted By Id
*/
List<Employee> sortById = customObjectSort.sortById(employeeList);
for (Employee employee : sortById) {
System.out.println(employee);
}
}
}
package in.ac.adit.oop.sort;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class CustomObjectSort {
public List<Employee> sortByName(List<Employee> employeeList) {
Collections.sort(employeeList, new Comparator<Employee>() {
@Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getName().compareTo(employee2.getName());
}
});
return employeeList;
}
public List<Employee> sortByDepartment(List<Employee> employeeList) {
Collections.sort(employeeList, new Comparator<Employee>() {
@Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getDepartment().compareTo(employee2.getDepartment());
}
});
return employeeList;
}
public List<Employee> sortById(List<Employee> employeeList) {
Collections.sort(employeeList, new Comparator<Employee>() {
@Override
public int compare(Employee employee1, Employee employee2) {
return employee1.getId() - employee2.getId();
}
});
return employeeList;
}
}