?
Solved

Sorting array data

Posted on 2005-04-01
13
Medium Priority
?
236 Views
Last Modified: 2008-03-10
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
Comment
Question by:acslater
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 13679238
Doesn't that work?

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

what errors do you get?
0
 

Author Comment

by:acslater
ID: 13679333
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13679352
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:acslater
ID: 13679359
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13679363
0
 

Author Comment

by:acslater
ID: 13679428
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13679442
Only the class definition should be in the <%! %> tags...  the rest should be in normal <% %> tags (like in my code)
0
 

Author Comment

by:acslater
ID: 13679563
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
 
LVL 35

Accepted Solution

by:
TimYates earned 1600 total points
ID: 13679575
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
 

Author Comment

by:acslater
ID: 13679603
Great no errors now... thanks.

How will I go about print the label information (eg Computer Application) to prove  they are sorted?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13679619
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
 

Author Comment

by:acslater
ID: 13679666
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13679678
Ahhh...swap the line:

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

to be

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

;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question