3

首先:我使用一个数组来获取这样的信息:

// 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。

非常感谢。

4

5 回答 5

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
    }
});

你也可以makeDateTime来执行Comparable。在这种情况下,只需调用Arrays.sort(array)

于 2013-05-29T09:28:01.190 回答
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
于 2013-05-29T09:27:51.890 回答
2

这里的其他答案很好,但使用过时的类。

java.time

在 Java 8 及更高版本中,我们现在内置了 java.time 框架(带有 Java 6 & 7 和 Android 的后向端口)。对旧的日期时间类的巨大改进。

java.time 类包括一对满足您确切需求的类:

通过预定义这些类型,您甚至不需要按照其他评论和答案的建议定义自己的类。至少如果您对天数排序的定义是周一到周日。枚举具有按该DayOfWeek顺序预定义的星期几。您可以创建自己的课程DayOfWeekLocalTime如果它对您的更大项目有意义的话。

Java 枚举非常方便、灵活且功能强大(如果您不熟悉,请了解更多信息)。枚举有自己的特殊实现Setand Map,适当命名,EnumSetand EnumMap。我们可以使用 anEnumMap来跟踪一周中的每一天,将其映射到一天中的时间(一个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 到 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
于 2016-04-10T09:23:23.037 回答
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 ...
    }
  });

}

但是,正如其他人在评论中所写的那样:我强烈建议您对问题采取更加面向对象的方法。

于 2013-05-29T09:39:11.283 回答
1

不要在此处使用具有某些您知道但 java 不知道的“虚拟类型结构”的数组。使用强类型类建模。实现Comparable排序。然后使用集合- 将对象实例添加到 aList并使用它的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();
于 2013-05-30T09:23:35.883 回答