0

I am having some issues sorting an array, I wish to sort the array by day which can be either Monday,Tuesday, Wednesday, Thursday or Friday. I have a class which displays everything within the array but I want to order this by the days above, how can I do this?

I've tried using collections.sort and Arrays.sort but doesn't seem to give me the desired effect, any ideas?

public void sortArray() {



}

04-23 12:55:49.010: E/AndroidRuntime(281): FATAL EXCEPTION: main 04-23 12:55:49.010: E/AndroidRuntime(281): java.lang.RuntimeException: Unable to start activity 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): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-23 12:55:49.010: E/AndroidRuntime(281): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 04-23 12:55:49.010: E/AndroidRuntime(281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 04-23 12:55:49.010: E/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:99) 04-23 12:55:49.010: E/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123) 04-23 12:55:49.010: E/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627) 04-23 12:55:49.010: E/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method) 04-23 12:55:49.010: E/AndroidRuntime(281): at 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): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-23 12:55:49.010: E/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method) 04-23 12:55:49.010: E/AndroidRuntime(281): Caused by: java.lang.NullPointerException 04-23 12:55:49.010: E/AndroidRuntime(281): at 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): at java.util.TimSort.countRunAndMakeAscending(TimSort.java:320) 04-23 12:55:49.010: E/AndroidRuntime(281): at 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): at java.util.Arrays.sort(Arrays.java:1907) 04-23 12:55:49.010: E/AndroidRuntime(281): at java.util.Collections.sort(Collections.java:1972) 04-23 12:55:49.010: E/AndroidRuntime(281): at org.assignment.timetable.Timetable.sortModules(Timetable.java:71) 04-23 12:55:49.010: E/AndroidRuntime(281): at org.assignment.timetable.Timetable.onCreate(Timetable.java:40) 04-23 12:55:49.010: E/AndroidRuntime(281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-23 12:55:49.010: E/AndroidRuntime(281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-23 12:55:49.010: E/AndroidRuntime(281): ... 11 more

5
  • 4
    use Arrays.sort() on your array, and create your own comparator. Commented Apr 23, 2012 at 11:21
  • is their any tutorials on this? Commented Apr 23, 2012 at 11:28
  • Google is your friend, here is one example of creating a comparator: vogella.com/blog/2009/08/04/collections-sort-java I'll throw together a quick example too. Commented Apr 23, 2012 at 11:31
  • Here's another mkyong.com/java/… Commented Apr 23, 2012 at 11:31
  • These both are sorting items when they are added i want to take the array already held and sort by day then display the new output Commented Apr 23, 2012 at 11:35

2 Answers 2

2

Here is an example:

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

And when you have created your array (any way you want) you can sort it like so:

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

Or if you have a list:

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

The items in your array will now be sorted.

Sign up to request clarification or add additional context in comments.

11 Comments

I made it like an inner class, but technically it could be defined anywhere.
getting this error and I had to remove the Override within MyComparator: The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (ArrayList<Module>, Timetable.MyComparator)
@mitchnufc First of all, the Arrays.sort() is for arrays, not lists, for a list (such as ArrayList) you would use Collections.sort() instead. Secondly, You didn't mention that you had a custom class for the days, so I assumed they where strings. Just get the type from your Module class and use that instead of the strings in the map, and replace <String> with <Module>
sorry for the confussion, so do I just stick the Module and replace the String in the HashMap only?
@mitchnufc I edited the example a bit. it's a little bit hard to make it good as I don't know how you store the day in your Module-class.
|
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;
}

}

I haven't tested this, but should work.. on paper (eh...screen)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.