0

我在排序数组时遇到了一些问题,我希望按天对数组进行排序,可以是星期一、星期二、星期三、星期四或星期五。我有一个显示数组中所有内容的类,但我想在上述日期之前订购它,我该怎么做?

我试过使用 collections.sort 和 Arrays.sort 但似乎没有给我想要的效果,有什么想法吗?

public void sortArray() {



}

04-23 12:55:49.010: E/AndroidRuntime(281): 致命异常: main 04-23 12:55:49.010: E/AndroidRuntime(281): java.lang.RuntimeException: 无法启动活动 ComponentInfo{org. assignment.timetable/org.assignment.timetable.Timetable}: java.lang.NullPointerException 04-23 12:55:49.010: E/AndroidRuntime(281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 04 -23 12:55:49.010: E/AndroidRuntime(281): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 android.app .ActivityThread.access$2300(ActivityThread.java:125) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 04-23 12: 55:49.010: E/AndroidRuntime(281): 在 android.os.Handler.dispatchMessage(Handler.java:99) 04-23 12:55:49.010:E/AndroidRuntime(281): 在 android.os.Looper.loop(Looper.java:123) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 android.app.ActivityThread.main(ActivityThread.java :4627) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-23 12:55:49.010: E/AndroidRuntime(281): 在java.lang.reflect.Method.invoke(Method.java:521) 04-23 12:55:49.010: E/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java :868) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-23 12:55:49.010: E/AndroidRuntime (281): 在 dalvik.system.NativeStart.main(Native Method) 04-23 12:55:49.010: E/AndroidRuntime(281): 由: java.lang.NullPointerException 04-23 12:55:49.010: E /AndroidRuntime(281):在 org.assignment.timetable。Timetable$MyComparator.compare(Timetable.java:51) 04-23 12:55:49.010: E/AndroidRuntime(281): at org.assignment.timetable.Timetable$MyComparator.compare(Timetable.java:1) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 java.util.TimSort.countRunAndMakeAscending(TimSort.java:320) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 java.util.TimSort .sort(TimSort.java:185) 04-23 12:55:49.010: E/AndroidRuntime(281): at java.util.TimSort.sort(TimSort.java:169) 04-23 12:55:49.010: E /AndroidRuntime(281): 在 java.util.Arrays.sort(Arrays.java:1907) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 java.util.Collections.sort(Collections.java: 1972) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 org.assignment.timetable.Timetable.sortModules(Timetable.java:71) 04-23 12:55:49.010: E/AndroidRuntime(281) : 在 org.assignment.timetable.Timetable.onCreate(Timetable.java:40) 04-23 12:55:49.010: E/AndroidRuntime(281): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-23 12:55:49.010: E/AndroidRuntime(281): 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-23 12:55:49.010: E/AndroidRuntime(281): ... 11 更多

4

2 回答 2

2

这是一个例子:

    //Assuming that the Module-class has the day stored as a String named "day"
static class MyComparator implements Comparator<Module>{
    private final static HashMap<String, Integer> order = getMap();

    @Override
    public int compare(Module lhs, Module rhs){
        int leftOrder = order.get(lhs.day.toLowerCase());
        int rightOrder = order.get(rhs.day.toLowerCase());
        return leftOrder - rightOrder;
    }

    private final static HashMap<String, Integer> getMap(){
        HashMap<String, Integer> res = new HashMap<String, Integer>();
        res.put("monday",0);
        res.put("tuesday",1);
        res.put("wednesday",2);
        res.put("thursday",3);
        res.put("friday",4);

        return res;
    }
}

当你创建了你的数组(任何你想要的方式)时,你可以像这样对它进行排序:

Arrays.sort(myArray, new MyComparator());

或者,如果您有一个清单:

Collections.sort(myList, new MyComparator());

现在将对数组中的项目进行排序。

于 2012-04-23T11:40:44.783 回答
1
public WeekComparator implements Comparator {
private String[] weeksdays = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
public int compare(Object o1, Object o2) {
  if(!(o1 instanceof String && o2 instanceof String)) {
    throw new ClassCastException();
  }
  int day1 = indexOfDay((String)o1);
  int day2 = indexOfDay((String)o2);
  if(day1==day2) return 0;
  return (day1>day2 ? 1 : -1);
}

private int indexOfDay(String s) {
  for(int i = 0 ; i < weekdays.length;i++) {
    if(weekdays[i].equalsIgnoreCase(s)) return i;
  return 10;
}

}

我没有对此进行测试,但应该可以工作..在纸上(嗯...屏幕)

于 2012-04-23T11:35:48.337 回答