Results 1 to 6 of 6

Thread: Arrays order

  1. #1
    XxGonsxX is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Post 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?

  2. #2
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default 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)

  3. #3
    XxGonsxX is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

    Default 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 :)!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default 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.

    Java 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

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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.

Similar Threads

  1. Order of numbers
    By Green in forum New To Java
    Replies: 2
    Last Post: 09-10-2011, 07:09 AM
  2. order of sequence
    By dannyy in forum New To Java
    Replies: 23
    Last Post: 04-14-2011, 08:29 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  4. How to order a vector?
    By tyang in forum New To Java
    Replies: 2
    Last Post: 02-06-2010, 09:24 AM
  5. Descending order
    By santanu in forum New To Java
    Replies: 1
    Last Post: 11-04-2008, 05:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •