首先:我使用数组来获取这样的信息:
// Tuesday
array[2][1] = "tuesday";
array[2][2] = "20:00";
// Wednesday
array[3][1] = "Wednesday";
array[3][2] = "15:00";
// Thursday
array[4][1] = "Thursday";
array[4][2] = "20:00";
// Friday
array[5][1] = "Friday";
array[5][2] = "18:00";
// Saturday
array[6][1] = "Saturday";
array[6][2] = "15:00";
// Sunday
array[7][1] = "Sunday";
array[7][2] = "15:00";
如何按实际时间和工作日对数组进行排序? 示例:现在是星期三 - 11:13。第一个Array-Item将是数组[3],然后是4,5,6,7,然后是2。
非常感谢。
答案 0 :(得分:6)
您应该使用Arrays.sort(array,comparator)
,例如像这样的东西:
Arrays.sort(array, new Comparator<String[]>() {
public int compareTo(String[] one, String[] two) {
// implement compareTo here
}
});
但是对于不同的数据使用二维数组而不是自定义类型的一维数组是非常不好的做法,即:
public class DayTime {
private String day;
private String time;
// constructors, setters, getters
}
现在创建这样的数组:
DayTime[] days = new DayTime[] {
new DayTime("tuesday", "20:00").
new DayTime("Wednesday", "15:00"),
// etc, etc
};
Arrays.sort(array, new Comparator<DayTime>() {
public int compareTo(DayTime one, DayTime two) {
// implement compareTo here
}
});
您还可以DateTime
实施Comparable
。在这种情况下,只需致电Arrays.sort(array)
答案 1 :(得分:3)
class CalendarEntry implements Comparable<CalendarEntry> {
String entry;
Date start;
// constructors, getters, setters
int compareTo(CalendarEntry o) {
if (o==null) return 1;
return start.compareTo(o.start);
}
}
List<CalendarEntry> entriesList = new ArrayList<CalendarEntry>();
// add contents
Collections.sort(entriesList);
// and you are done
答案 2 :(得分:2)
这里的其他答案很好,但是使用过时的类。
使用Java 8及更高版本,我们现在内置了java.time框架(带有Java 6&amp; 7和Android的后端口)。与旧的日期时间类相比有了很大的改进。
java.time类包含一对符合您确切需求的类:
预先定义了这些类型,您甚至不需要像其他注释和答案所建议的那样定义自己的类。至少如果您对日期排序的定义在周一至周日进行。 DayOfWeek
枚举按此顺序预定了几周的天数。如果您的更大项目有意义, 可以组建DayOfWeek
和LocalTime
。
Java enums 非常方便,灵活且功能强大(如果对您不熟悉,请learn more)。枚举有Set
和Map
的特殊实现,适当命名,EnumSet
和EnumMap
。我们可以使用EnumMap
来跟踪一周中的每一天,将其映射到时间(LocalTime
对象)。
EnumMap<DayOfWeek , LocalTime> dayToTimeMap = new EnumMap<> ( DayOfWeek.class );
dayToTimeMap.put ( DayOfWeek.TUESDAY , LocalTime.parse ( "20:00" ) );
dayToTimeMap.put ( DayOfWeek.WEDNESDAY , LocalTime.of ( 15 , 0 ) );
dayToTimeMap.put ( DayOfWeek.THURSDAY , LocalTime.parse ( "20:00" ) );
dayToTimeMap.put ( DayOfWeek.FRIDAY , LocalTime.parse ( "18:00" ) );
dayToTimeMap.put ( DayOfWeek.SATURDAY , LocalTime.parse ( "15:00" ) );
获取当前的星期几和时间。
DayOfWeek today = DayOfWeek.WEDNESDAY;
LocalTime now = LocalTime.of ( 11 , 13 );
创建一对空集,以跟踪与现在相同或晚于现在的日期,以及跟踪较早日期的日期。作为EnumSet
,它们的自然顺序是在DayOfWeek
枚举(周一至周日,1-7)中声明的顺序。
EnumSet<DayOfWeek> earlier = EnumSet.noneOf ( DayOfWeek.class );
EnumSet<DayOfWeek> later = EnumSet.noneOf ( DayOfWeek.class );
循环DayOfWeek-to-LocalTime地图。查看DayOfWeek是否在今天之前,等于或晚于今天。如果等于今天,则将其LocalTime
对象与我们的now
对象进行比较。将此DayOfWeek对象分配给earlier
集或later
集。
for ( Map.Entry<DayOfWeek , LocalTime> entry : dayToTimeMap.entrySet () ) {
DayOfWeek key = entry.getKey ();
LocalTime value = entry.getValue ();
int comparison = key.compareTo ( today );
if ( comparison < 0 ) { // if earlier day…
earlier.add ( key );
} else if ( comparison == 0 ) { //If same day…
if ( value.isBefore ( now ) ) {
earlier.add ( key );
} else { // Else same time as now or later than now…
later.add ( key );
}
} else if ( comparison > 0 ) {
later.add ( key );
} else {
throw new RuntimeException ( "Unexpectedly reached IF-ELSE for comparison: " + comparison );
}
}
转储到控制台。我们希望首先循环later
集,然后根据问题中列出的要求循环earlier
集。
System.out.println ( "dayToStringMap: " + dayToTimeMap );
System.out.println ( "sorted by today: " + today + " " + now + " is: " );
for ( DayOfWeek dayOfWeek : later ) {
LocalTime localTime = dayToTimeMap.get ( dayOfWeek );
System.out.println ( dayOfWeek + " " + localTime );
}
for ( DayOfWeek dayOfWeek : earlier ) {
LocalTime localTime = dayToTimeMap.get ( dayOfWeek );
System.out.println ( dayOfWeek + " " + localTime );
}
运行时。
dayToStringMap: {TUESDAY=20:00, WEDNESDAY=15:00, THURSDAY=20:00, FRIDAY=18:00, SATURDAY=15:00}
sorted by today: WEDNESDAY 11:13 is:
WEDNESDAY 15:00
THURSDAY 20:00
FRIDAY 18:00
SATURDAY 15:00
TUESDAY 20:00
答案 3 :(得分:1)
可以使用Arrays.sort
和您自己的Comparator
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
public static void main(String[] args) {
String[][] array = createArray();
Arrays.sort(array, new Comparator<String[]>() {
@Override public int compare(String[] o1, String[] o2) {
String day1 = o1[0];
String day2 = o2[0];
String time1 = o1[1];
String time2 = o2[1];
// Perform comparison, first of the days, then - if they're
// identical of the times.
return ...
}
});
}
然而,正如其他人在评论中所写:我强烈建议您采取更加面向对象的方法解决问题。
答案 4 :(得分:1)
不要在这里使用一些已知的“虚拟类型结构”,而不是java。使用强类型类建模。实施Comparable
进行排序。然后使用collections - 将对象实例添加到List
并使用它的sort
方法。
class Event implements Comparable<Event> {
String day;
String time;
public Event(String day, String time) { this.day = day; this.time = time; }
public String getDay() { return this.day; }
public String getTime() { return this.time; }
@Override
public boolean equals(Object other) {
boolean result = false;
if (other != null && other instanceof Event) {
Event otherEvent = (Event)other;
result = this.getDay().equals(otherEvent.getDay()) &&
this.getTime().equals(otherEvent.getTime());
}
return result;
}
@Override
public int hashCode() {
return this.getDay().hashCode()*7 + this.getDay().hashCode();
}
@Override
public int compareTo(Event otherEvent) {
int result = this.getDay().compareTo(otherEvent.getDay());
if (result == 0) result = this.getTime().compareTo(otherEvent.getTime());
return result;
}
}
然后在其他一些类或主要方法中:
List<Event> eventList = new ArrayList<Event>();
eventList.add(new Event("tuesday","20:00"));
eventList.add(new Event("tuesday","20:00"));
// etc
eventList.sort();