EDU-717 Adaptive/Nonadaptive sorting breaks "Preview Course" action
[idea/community.git] / python / educational-core / student / src / com / jetbrains / edu / learning / courseGeneration / StudyProjectGenerator.java
index b512f91cfa3b7df7d0df9cf94ba94352356f0855..0a12c9558fdd223d0a53070bc11a3aca7fb5a550 100644 (file)
@@ -407,10 +407,28 @@ public class StudyProjectGenerator {
     if (myCourses.isEmpty()) {
       myCourses = getBundledIntro();
     }
-    StudyUtils.sortCourses(myCourses);
+    sortCourses(myCourses);
     return myCourses;
   }
 
+  public void sortCourses(List<CourseInfo> result) {
+    // sort courses so as to have non-adaptive courses in the beginning of the list
+    Collections.sort(result, (c1, c2) -> {
+      if (mySelectedCourseInfo != null) {
+        if (mySelectedCourseInfo.equals(c1)) {
+          return -1;
+        }
+        if (mySelectedCourseInfo.equals(c2)) {
+          return 1;
+        }
+      }
+      if ((c1.isAdaptive() && c2.isAdaptive()) || (!c1.isAdaptive() && !c2.isAdaptive())) {
+        return 0;
+      }
+      return c1.isAdaptive() ? 1 : -1;
+    });
+  }
+
   @NotNull
   public List<CourseInfo> getCoursesUnderProgress(boolean force, @NotNull final String progressTitle, @NotNull final Project project) {
     try {
@@ -539,7 +557,7 @@ public class StudyProjectGenerator {
       File courseFile = courseFiles[0];
       CourseInfo courseInfo = getCourseInfo(courseFile);
       if (courseInfo != null) {
-        courses.add(courseInfo);
+        courses.add(0, courseInfo);
       }
       return courseInfo;
     }