Reputation: 97
So I've created a simple program that asks a user to enter 5 genres and then score them out of 10. I've not added any validation, but I'm not worried about that. As you can see, I have two arrays: genres[]
and score[]
. Let's say they enter:
[1] : Genre A | 3
[2] : Genre B | 6
[3] : Genre C | 2
[4] : Genre D | 10
[5] : Genre E | 8
The results should be listed Genre D, E, B, A, C
Here's my overall code:
import java.util.Scanner;
class OrigClass {
public static void main (String[] args){
Scanner ScanObj = new Scanner(System.in);
int count;
String[] genres;
genres = new String[5];
int[] score;
score = new int[5];
for (count = 0; count < 5;count++){
System.out.println("Enter A Genre: ");
genres[count] = ScanObj.nextLine();
System.out.println("How much do you like it? ");
score[count] = ScanObj.nextInt();
ScanObj.nextLine();
}
for (count = 0; count < 5; count++){
System.out.println(count+1 + ") " + genres[count] + " " + score[count] + "/10");
}
ScanObj.close();
}
}
Is there a clever way to do it in java using some functions or would I have to manually do it by using temporary varibales, if statements etc. I suppose I could also use the bubble sort algorithm which would be fairly easy to implement. So I want to sort the contents of score[]
into descending order. Any tips would be appreciated.
Upvotes: 1
Views: 4494
Reputation: 5176
i would recommend few changes in your code as follows
step 1.
Make a bean named Genre with the following thing
String name int score
now create in your main class create an ArrayList
now add the genres as objects
step2.
now let your bean implement the comparable interface and use the sort method on the objects made in.the main.class and and overide the method of the comparable interface
i guess it will be compareTo so in tht method compare the scores and it will sorted...
Upvotes: 0
Reputation: 234795
Rather than having two parallel arrays, create an array of genre/score objects.
class OrigClass {
class ScoredGenre {
public String genre;
public int score;
}
ScoredGenre[] data = new ScoredGenre[5];
. . .
}
Then you can define a comparator to compare the objects by score and sort accordingly.
Arrays.sort(data, new Comparator<ScoredGenre>() {
public int compare(ScoredGenre a, ScoredGenre b) {
// Note that a and b are reversed to obtain a descending sort
return Integer.compare(b.score, a.score);
}
});
Upvotes: 5
Reputation: 68715
Try the method Arrays.sort(Object[]);
From the javadoc:
sort
public static void sort(Object[] a) Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array). This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance.
Upvotes: 0