• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Sorting array data

Want to sort data (int - result of calculation) that is stored in an array and put a lable on the data
This is the code am using:

<%
   public class Score {
      private int score = 0;
      private String courseName = null;

      public Score(int s, String n) {
        score = s;
        courseName = n;
      }

     public int getScore() { return score; }
     public String getCourseName() { return courseName; }
     public void setScore(int s) { score = s; }
     public void setCourseName(String n) { courseName = n; }
  }

//[2] Store the results in:

List scoresList = new ArrayList();
scoresList.add(new Score(scores[0], "Computer Application"));
scoresList.add(new Score(scores[1], "MultiMedia"));
scoresList.add(new Score(scores[2], "Applied Commercial Computing"));
scoresList.add(new Score(scores[3], "Applied Industrial Computing"));
scoresList.add(new Score(scores[4], "Commercial Computing"));

<%
//[3] Then to sort:

Collections.sort(scoresList, new Comparator() {
    public int compareTo(Object o1, Object o2) {
        Score s1 = (Score)o1;
        Score s2 = (Score)o2;
     return s1.getScore()-s2.getScore();  
    }
});

%>

0
acslater
Asked:
acslater
  • 7
  • 6
1 Solution
 
TimYatesCommented:
Doesn't that work?

A brief look at it says it should do...

what errors do you get?
0
 
acslaterAuthor Commented:
Error Msg is:

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 3 in the jsp file: /p5test1.jsp

Generated servlet error:
    [javac] Compiling 1 source file

/home/combcdte/tomcat/work/testp6_jsp.java:423: illegal start of expression
   public class Score {
   ^

An error occurred at line: 3 in the jsp file: /p5test1.jsp

Generated servlet error:
/home/combcdte/tomcat/work/testp6_jsp.java:423: ';' expected
   public class Score {
          ^
2 errors
0
 
TimYatesCommented:
Ahhh..right...you need to define classes in special blocks with JSPs  ( <%! code %> ) Notice the exclamation mark "!"

Try this:

<%!
   public class Score {
      private int score = 0;
      private String courseName = null;

      public Score(int s, String n) {
        score = s;
        courseName = n;
      }

     public int getScore() { return score; }
     public String getCourseName() { return courseName; }
     public void setScore(int s) { score = s; }
     public void setCourseName(String n) { courseName = n; }
  }
%>

<%
  //[2] Store the results in:

  List scoresList = new ArrayList();
  scoresList.add(new Score(scores[0], "Computer Application"));
  scoresList.add(new Score(scores[1], "MultiMedia"));
  scoresList.add(new Score(scores[2], "Applied Commercial Computing"));
  scoresList.add(new Score(scores[3], "Applied Industrial Computing"));
  scoresList.add(new Score(scores[4], "Commercial Computing"));

  //[3] Then to sort:

  Collections.sort(scoresList, new Comparator() {
      public int compareTo(Object o1, Object o2) {
          Score s1 = (Score)o1;
          Score s2 = (Score)o2;
       return s1.getScore()-s2.getScore();  
      }
  });
%>
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
acslaterAuthor Commented:
The aim is to sort data (int - result of calculation) that is stored in a List and out the label (and not the value in the array).
0
 
TimYatesCommented:
0
 
acslaterAuthor Commented:
OK noticed I was not starting with <%! and ending with %>, but new errors(6) now...  and all seem to be the same:

/home/combcdte/tomcat/work/testp6_jsp.java:34: <identifier> expected
scoresList.add(new Score(scores[0], "Computer Application"));
              ^
/home/combcdte/tomcat/work/testp6_jsp.java:35: <identifier> expected
scoresList.add(new Score(scores[1], "MultiMedia"));
              ^
/home/combcdte/tomcat/work/testp6_jsp.java:36: <identifier> expected
scoresList.add(new Score(scores[2], "Applied Commercial Computing"));
              ^
/home/combcdte/tomcat/work/testp6_jsp.java:37: <identifier> expected
scoresList.add(new Score(scores[3], "Applied Industrial Computing"));
              ^
/home/combcdte/tomcat/work/testp6_jsp.java:38: <identifier> expected
scoresList.add(new Score(scores[4], "Commercial Computing"));
              ^
/home/combcdte/tomcat/work/testp6_jsp.java:44: <identifier> expected
Collections.sort(scoresList, new Comparator() {
                ^
6 errors
0
 
TimYatesCommented:
Only the class definition should be in the <%! %> tags...  the rest should be in normal <% %> tags (like in my code)
0
 
acslaterAuthor Commented:
Thanks... Down to one error:

/home/combcdte/tomcat/work/testp6_jsp.java:455: <anonymous org.apache.jsp.testp6_jsp$1> is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.Comparator
    public int compareTo(Object o1, Object o2) {
               ^
Note: /home/combcdte/tomcat/work/testp6_jsp.java uses or overrides a deprecated API.
Note: Recompile with -deprecation for details.
1 error
0
 
TimYatesCommented:
You need to use

      public int compare(Object o1, Object o2) {

not

      public int compareTo(Object o1, Object o2) {

so:

  Collections.sort(scoresList, new Comparator() {
      public int compare(Object o1, Object o2) {
          Score s1 = (Score)o1;
          Score s2 = (Score)o2;
       return s1.getScore()-s2.getScore();  
      }
  });

0
 
acslaterAuthor Commented:
Great no errors now... thanks.

How will I go about print the label information (eg Computer Application) to prove  they are sorted?
0
 
TimYatesCommented:
for( int i = 0 ; i < scoresList.size() ; i++ )
{
    Score score = (Score)scoresList.get( i ) ;
    out.println( score.getCourseName() + " (" + score.getScore() + ")" ) ;
    out.println( "<br>" ) ;
}

should do it :-)
0
 
acslaterAuthor Commented:
Great that works ... just had to change i to another letter, because I have used i earlier.

The only thing is sorts in reverse order and we need them in ascending order.

Applied Industrial Computing (56)
Commercial Computing (58)
MultiMedia (60)
Computer Application (62)
Applied Commercial Computing (63)

0
 
TimYatesCommented:
Ahhh...swap the line:

       return s1.getScore()-s2.getScore();  

to be

       return s2.getScore()-s1.getScore();  

;-)
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now