Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Comparing integers

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to compare a 'rank' score of speech competitors in my program
the following code alphabatises the names of the people


is there any way i could assign a new array RankGroup to debateGroup and and make a copy of this for loop and use it to rank the students by there rank score value? and i know that compareto. only works with string....is there something that compares ints....if anyone has any ideas on i how would rank the scores by least pt value being first place...pls help
thanks
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's something to play around with
 
Sally Curtis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for the code, but im a beginner java student and im bit confused with the code. can u pls explain the code ?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first part just deals with creating a few 'Students'.
Then they're sorted by 'mark' (or in your terminology, rank)
Then the output (should be) in sorted order.
The java API will do a better job than I can about how Collections.sort
and Comparator work.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bubble sort would do the trick.
 
lowercase baba
Posts: 13023
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming debateGroup is an array of objects that contain names and grades, and that you have the appropriate methods, you could use getScore() instead of getLastName()?
your while condition would change to something like
while(ts > 0 && debateGroup[ts-1].getScore() < debateGroup[ts].getScore())
 
Ranch Hand
Posts: 268
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend you read up on the Java Collections API, particularly the sorting mechanisms based on Comparator. It sounds to me like all you have to do is implement a couple of different subclasses for Comparator, and you can use the Java Collections API to do all your sorting for you.
If this isn't enough direction and you need a few more pointers getting started on this, let me know, but read the javadocs first on the API and classes mentioned above.
sev
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic