Reputation: 1
So I had a delimited file that I read into an array.
array[0] is the boxID (double)
and
array[1] is the movieID (double)
I have no clue how I'd be able to sort my array by these two doubles. Any comments? I've tried looking at other questions on this website but I just got confused by them. I'm currently in my first programming class.
Movies[] newMasterMovies = new Movies[200];
int newMasterCount = 0;
int masterCount = 0;
int updateCount = 0;
while (updateCount < updateTotalCounter || masterCount < masterTotalCounter) {
String updateCompare = updateMovies[updateCount].getBoxID() + updateMovies[updateCount].getMovieID();
String masterCompare = masterMovies[masterCount].getBoxID() + masterMovies[masterCount].getMovieID();
int compare = updateCompare.compareTo(masterCompare);
if (compare > 0) {
newMasterMovies[newMasterCount] = masterMovies[masterCount];
masterCount++;
newMasterCount++;
}
if (updateMovies[updateCount].getActionCode() == "A") {
newMasterMovies[newMasterCount] = updateMovies[updateCount];
updateCount++;
newMasterCount++;
}
if (updateMovies[updateCount].getActionCode() == "D") {
updateCount++;
masterCount++;
}
if (updateMovies[updateCount].getActionCode() == "C") {
newMasterMovies[newMasterCount] = updateMovies[updateCount];
updateCount++;
newMasterCount++;
masterCount++;
}
}
That is what my array looks like that I am trying to sort. I tried to do a selection sort but got confused since I want to sort by two properties, not just one.
Upvotes: 0
Views: 79
Reputation: 134
To simply sort your Movies[]
you can use Arrays.sort
and use a custom Comparator
. Like this:
Arrays.sort(masterMovies, new Comparator<Movies>() {
@Override
public int compare(Movies o1, Movies o2) {
// compare boxID
// compare movieID
return 0; // return result
}
});
Arrays.sort use merge sort
or binary insertion sort
which are both more stable and faster than selection sort
.
If you insist to do your selection sort, try to edit yor class Movies
to implement Comparable
interface, like this: class Movies implements Comparable<Movies>
. And implement compareTo
method like this:
@Override
public int compareTo(Movies o) {
// compare boxID
// compare movieID
return 0; // return result
}
And change your old compare code int compare = updateCompare.compareTo(masterCompare);
to int compare=updateMovies[updateCount].compareTo(masterMovies[masterCount]);
, then go on.
Upvotes: 0
Reputation: 2743
This guy here does a wonders
Arrays.sort(iArr);
Here is what it can do: Here is an example code
public class ArrayDemo {
public static void main(String[] args) {
// initializing unsorted int array
int iArr[] = {2, 1, 9, 6, 4};
// let us print all the elements available in list
for (int number : iArr) {
System.out.println("Number = " + number);
}
// sorting array
Arrays.sort(iArr);
// let us print all the elements available in list
System.out.println("The sorted int array is:");
for (int number : iArr) {
System.out.println("Number = " + number);
}
}
}
And the results should be like this
Number = 2
Number = 1
Number = 9
Number = 6
Number = 4
The sorted int array is:
Number = 1
Number = 2
Number = 4
Number = 6
Number = 9
Hopes this helps some
Upvotes: 1