我有一个对象数组,每个对象都分配了一个日期,我怎样才能按日期顺序对这些对象进行排序?
I have an array of objects and each object has been assigned a date, how can I sort these objects in order of date?
我目前有一个数组,其中包含名为 'People' 的对象。每个人都有名字、日、月、年(出生)。我想将它们转换为日期并使用 .isAfter() 方法比较它们并求助它们,但它根本不按日期对它们进行排序。这是代码的更简单版本
for (int i = 0; i<peopleArray.size()-1; i++)
{
for (int j = 0; j<peopleArray.size()-1; j++)
{
LocalDate firstDate = LocalDate.of(Integer.parseInt(peopleArray.get(i).getDOBYear()),
Integer.parseInt(peopleArray.get(i).getDOBMonth()),
Integer.parseInt(peopleArray.get(i).getDOBDay()));
LocalDate secondDate= LocalDate.of(Integer.parseInt(peopleArray.get(j).getDOBYear()),
Integer.parseInt(peopleArray.get(j).getDOBMonth()),
Integer.parseInt(peopleArray.get(j).getDOBDay()));
if(firstDate.isAfter(secondDate))
{
Person temp = peopleArray[i];
peopleArray[i] = peopleArray[i+1];
peopleArray[i+1] = temp;
}
}
}
'Person' 是对象的名称。非常感谢您的提前帮助!
按照duffymo
的建议,您只需在People
class上实现Comparable
接口,如下所示:
public class People implements Comparable<People> {
/* Members */
private String name;
private int dobDay;
private int dobMonth;
private int dobYear;
/* Getter, Setter, Constructor */
/* Get Date Function */
private Date getDate() {
return LocalDate.of(this.dobYear, this.dobMonth, this.dobDay);
}
@Override
public int compareTo(People people) {
return this.getDate().isAfter(people.getDate());
}
}
然后最后按如下方式对数组进行排序:
Collections.sort(peopleArray);
如果您在 java 中寻找更简单的解决方案,那么 user2004685
的解决方案应该可以。
由于您已标记 bubble-sort
,我猜您需要帮助解决代码中的问题。
问题:
- 循环将永远不会覆盖列表中的最后一个元素。它应该 运行 直到
i/j < size()
.
- 交换块交换与比较元素不同的元素。您正在比较
i
和 j
位置的元素,但交换 i
和 i+1
- 我假设使用下标运算符访问数组的交换代码正在处理另一个变量而不是列表变量(因为下标不适用于列表)。
- 您可以通过减少循环持续时间来增强循环。请参考提供的冒泡排序代码 here 以供参考。
更正代码:
for (int i = 0; i<peopleArray.size(); i++)
{
for (int j = 0; j<peopleArray.size(); j++)
{
LocalDate firstDate = LocalDate.of(Integer.parseInt(peopleArray.get(i).getDOBYear()),
Integer.parseInt(peopleArray.get(i).getDOBMonth()),
Integer.parseInt(peopleArray.get(i).getDOBDay()));
LocalDate secondDate= LocalDate.of(Integer.parseInt(peopleArray.get(j).getDOBYear()),
Integer.parseInt(peopleArray.get(j).getDOBMonth()),
Integer.parseInt(peopleArray.get(j).getDOBDay()));
if(firstDate.isAfter(secondDate))
{
Person temp = peopleArray[i];
peopleArray[i] = peopleArray[j];
peopleArray[j] = temp;
}
}
}
正如 user2004685 和 duffymo 所说,您需要编写一个自定义 compareTo
,示例 user2004685 对我来说是正确的(我目前这台计算机上没有编译器)。
我建议的一个附录是,您可能不想要 People
class - 也许您只想要现在拥有的数组,并且您并不总是想要对实例进行排序Person
按照您描述的方式。如果是这样,要做的就是将 Comparator
传递给 Arrays.sort()
:
Arrays.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
// implement Person.getDOB() in the appropriate way
return p1.getDOB().compareTo(p2.getDOB());
}});
或者,如果您使用 Java8,您可以使用 lambda 更简洁地执行此操作:
Arrays.sort(people, (p1,p2) -> p1.getDOB().compareTo(p2.getDOB()));
我目前有一个数组,其中包含名为 'People' 的对象。每个人都有名字、日、月、年(出生)。我想将它们转换为日期并使用 .isAfter() 方法比较它们并求助它们,但它根本不按日期对它们进行排序。这是代码的更简单版本
for (int i = 0; i<peopleArray.size()-1; i++)
{
for (int j = 0; j<peopleArray.size()-1; j++)
{
LocalDate firstDate = LocalDate.of(Integer.parseInt(peopleArray.get(i).getDOBYear()),
Integer.parseInt(peopleArray.get(i).getDOBMonth()),
Integer.parseInt(peopleArray.get(i).getDOBDay()));
LocalDate secondDate= LocalDate.of(Integer.parseInt(peopleArray.get(j).getDOBYear()),
Integer.parseInt(peopleArray.get(j).getDOBMonth()),
Integer.parseInt(peopleArray.get(j).getDOBDay()));
if(firstDate.isAfter(secondDate))
{
Person temp = peopleArray[i];
peopleArray[i] = peopleArray[i+1];
peopleArray[i+1] = temp;
}
}
}
'Person' 是对象的名称。非常感谢您的提前帮助!
按照duffymo
的建议,您只需在People
class上实现Comparable
接口,如下所示:
public class People implements Comparable<People> {
/* Members */
private String name;
private int dobDay;
private int dobMonth;
private int dobYear;
/* Getter, Setter, Constructor */
/* Get Date Function */
private Date getDate() {
return LocalDate.of(this.dobYear, this.dobMonth, this.dobDay);
}
@Override
public int compareTo(People people) {
return this.getDate().isAfter(people.getDate());
}
}
然后最后按如下方式对数组进行排序:
Collections.sort(peopleArray);
如果您在 java 中寻找更简单的解决方案,那么 user2004685
的解决方案应该可以。
由于您已标记 bubble-sort
,我猜您需要帮助解决代码中的问题。
问题:
- 循环将永远不会覆盖列表中的最后一个元素。它应该 运行 直到
i/j < size()
. - 交换块交换与比较元素不同的元素。您正在比较
i
和j
位置的元素,但交换i
和i+1
- 我假设使用下标运算符访问数组的交换代码正在处理另一个变量而不是列表变量(因为下标不适用于列表)。
- 您可以通过减少循环持续时间来增强循环。请参考提供的冒泡排序代码 here 以供参考。
更正代码:
for (int i = 0; i<peopleArray.size(); i++)
{
for (int j = 0; j<peopleArray.size(); j++)
{
LocalDate firstDate = LocalDate.of(Integer.parseInt(peopleArray.get(i).getDOBYear()),
Integer.parseInt(peopleArray.get(i).getDOBMonth()),
Integer.parseInt(peopleArray.get(i).getDOBDay()));
LocalDate secondDate= LocalDate.of(Integer.parseInt(peopleArray.get(j).getDOBYear()),
Integer.parseInt(peopleArray.get(j).getDOBMonth()),
Integer.parseInt(peopleArray.get(j).getDOBDay()));
if(firstDate.isAfter(secondDate))
{
Person temp = peopleArray[i];
peopleArray[i] = peopleArray[j];
peopleArray[j] = temp;
}
}
}
正如 user2004685 和 duffymo 所说,您需要编写一个自定义 compareTo
,示例 user2004685 对我来说是正确的(我目前这台计算机上没有编译器)。
我建议的一个附录是,您可能不想要 People
class - 也许您只想要现在拥有的数组,并且您并不总是想要对实例进行排序Person
按照您描述的方式。如果是这样,要做的就是将 Comparator
传递给 Arrays.sort()
:
Arrays.sort(people, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
// implement Person.getDOB() in the appropriate way
return p1.getDOB().compareTo(p2.getDOB());
}});
或者,如果您使用 Java8,您可以使用 lambda 更简洁地执行此操作:
Arrays.sort(people, (p1,p2) -> p1.getDOB().compareTo(p2.getDOB()));