-
Arrays order
Hello! I'm starting with java programming!
I have a question about how to order arrays!
i have 2 arrays of some movies, 1 with the name and another one with the duration:
String Movies[]={"lala", "abc", "jejejs"};
int Duration[]={90,80,100};
the Duration[0]= the duration of the movie in [0], like that. But when I order the Movies by alphabethic order the durations don't "follow" the movies:
Arrays.sort (Movies); and it goes like "abc" has 90 duration instead of 80.
Is there any way I can make the correct durations match the correct movies?
-
Re: Arrays order
So your asking if you can have two different arrays of different types to be connected?
Why not make a movie class that holds data about each movie and create a movie instance with a name and duration as its parameters.
(OOP eat your heart out)
-
Re: Arrays order
Yes! That's correct sir, is there any way of connecting both arrays, so that when i order one that "int" goes for the same order :)!
-
Re: Arrays order
I wrote a blog article once (see the blog entries on this forum) on the heap sort algorithm; my implementation can sort two (or) more arrays as if they were 'coupled'; a.a.m.o.f. it can sort anything that implements a simple interface.
kind regards,
Jos
-
Re: Arrays order
If you want to implement jammas method you need to implement Comparator and set the compare method so that java will know how to sort your class objects based on what variable you want to sort it by.
Code:
class MyObject implements Comparator {
int size;
String name;
MyObject(String s, int i) {
size = i;
name=s;
}
public int compare(Object obj, Object obj2) {
if (obj instanceof MyObject && obj2 instanceof MyObject) {
MyObject myObj = (MyObject) obj;
MyObject myObj2 = (MyObject) obj2;
return (myObj.size>myObj2.size)? 1
: (myObj.size<myObj2.size)? -1 : 0;
} else return 0;
}
}
After that you can use Arrays.sort(myArray) to sort your objects using your comparator
-
Re: Arrays order
@OP: It might help if you said what the context is.
If you want control over the sorting algorithm try Jos' suggestion extended to whatever sort algorithm you want to apply. If you are prepared to use the Java library sorting implementations then do what others have suggested and replace your parallel arrays with a single class that implements Comparable.
But a third possibility exists. Perhaps this is a homework problem where you have been introduced to some some sort algorithm or other and are now being asked to implement it so that it works on two arrays "in parallel", ie at the same time. (*) As with any homework, nobody here is going to give you the code (that would be taking away all the fun!) but you should say what you've done and how your code is failing to have the desired effect.
-----
(*) @any teachers reading this: there is a special place in hell reserved for you if you set homework based on manipulating parallel arrays except as a prelude to explaining how Java does things. You will be tormented by daemons, the parallel array elements represented figuratively by the parallel prongs of their pitch forks.