0

Firstly, my apologies for my weak knowledge of Java and its terminology. I'll do my best. I'm trying to sort the array below, eventStuff, by the variables stored within the array. I am giving the user three options to choose from, an event number (Letter, number x 3), the number of guests entered (between 5 and 100, including), and an event type (1 to 4, including). I am currently suffering a complete mental block on how to do this. Have I set up the array incorrectly, or is there an easier way to do this using what I've got so far? Thank you for the assistance!

Update: So I've got a handle on all the numeric sorting, but the alphanumeric (e.g. A111) eventCodeString is beyond me. I have checked several forums and applied many reasonable solutions, but they are either ineffective or throw an error. I am unsure of what I should do, at this point.

package Chapter9;
import Chapter9.Event;
import javax.swing.JOptionPane;
import java.util.Arrays;
public class EventDemo
{

    public static void main(String[] args)
    {
        callMotto();
                int x;
                Event[] eventStuff = new Event[8];
                for(x = 0; x < 8; ++x)
                {
                    eventStuff[x] = new Event();
                    eventStuff[x].setEventCodeString();
                    eventStuff[x].setGuests();
                    eventStuff[x].setContactNumber();
                    eventStuff[x].setEventStr();
                }
                //Sorting method start
                    String sorting;
                    int sortMethod;

                    sorting = JOptionPane.showInputDialog("Please chooose sorting method:\n"
                            + "1 to sort by event number\n"
                            + "2 to sort by number of guests\n"
                            + "3 to sort by event type");
                    sortMethod = Integer.parseInt(sorting);
                    //Event number sorting start
                    if(sortMethod == 1)
                    {

                    }
                    //Event number sorting end
                    //Event guest sorting Start
                    if(sortMethod == 2)
                    {

                    }
                    //Event guest sorting end
                    //Event type sorting start
                    if(sortMethod == 3)
                    {

                    }
                    //Event type sorting end
                //Sorting method end
    }

    public static void callMotto()
    {

        JOptionPane.showMessageDialog(null, "*******************************************************\n"
                        +   "* Carly's Makes The Food That Makes The Party! *\n"
                        +   "*******************************************************");

    }

    }   
  • 1
    Since we can't see the `setEventCodeString`, etc., methods, we can't tell if the array is set up correctly, but it's probably OK. Having a separate line to call `new` for each array element looks bad: why not `eventStuff[x] = new Event();` inside the loop? To provide different sorting methods, you will need different `Comparator`s for the `Event`. Please read [this tutorial](http://docs.oracle.com/javase/tutorial/collections/interfaces/order.html) and especially the section on `Comparator`. – ajb Apr 04 '14 at 17:14
  • Excellent, I will study up on the Comparator immediately. And I will revise my code to reflect your suggestion. I did try the `eventStuff[x] = new Event();` originally, but received an odd error. Instead of troubleshooting, I went around it. Bad show. – Corey McDonald Apr 04 '14 at 17:18
  • 1
    @Blaine No, that makes no difference whatsoever. – Jason C Apr 04 '14 at 17:23

2 Answers2

6

Use the Arrays.sort() that takes a Comparator, then, depending on the selection, create a Comparator<Event> that compares two Events based on the selected sort key, e.g.:

class EventNumberComparator implements Comparator<Event> {
    @Override public int compare (Event a, Event b) {
        // for example:
        return Integer.compare(a.getEventNumber(), b.getEventNumber()); 
        // optionally: be sure to handle nulls if null Events are possible
    }
}

Then, if the user selects event number:

Arrays.sort(theArray, new EventNumberComparator());

Or, e.g.:

Comparator<Event> comparator = null;

if (sortMethod == 1)
    comparator = new EventNumberComparator();
// etc...

if (comparator != null)
    Arrays.sort(theArray, comparator);

Or any one of many possible versions of the above; point being that a Comparator compares two Events based on some key, and you then use that Comparator to define the sort order of the array. Create a copy of the array first and sort that if you'd rather not sort in place, although it seems like that will be fine for your application. You can also use containers, e.g. an ArrayList, if you'd like.

Check out the official tutorial on comparators (see the comparator section towards the bottom, not the "comparable" part at the top).

Jason C
  • 38,729
  • 14
  • 126
  • 182
3

For example, if you want to sort your array over number of guests, you can use something like that:

    Arrays.sort(eventStuff, new Comparator<Event>() {

        @Override
        public int compare(Event o1, Event o2) {

            if (o1.getGuests() < o2.getGuests())
                return -1;
            else if (o1.getGuests() == o2.getGuests())
                return 0;
            else
                return 1;

        }                                   
    });

I suppose that getGuests returns the number of guests.

PetrS
  • 1,110
  • 14
  • 38
  • +1 an anonymous class may be more to the OP's liking. – Jason C Apr 04 '14 at 17:22
  • 1
    There are built-in methods like [`Integer.compare`](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#compare(int,int)) that do the work you've done in your example, so that would make things even simpler. But since the OP is still learning, it's good to show the details of how things work. – ajb Apr 04 '14 at 17:34
  • So I've garnered a very, VERY basic understanding of how the `Comparator<>` works, and in fact have implemented it on two of my three sorting fields. However, my `eventCodeString`s are in alphanumeric format (e.g. A111). For this case, how could I compare them? – Corey McDonald Apr 04 '14 at 18:51
  • Do you need to sort your strings lexicographically? – PetrS Apr 04 '14 at 19:33
  • Yes, as long as my understand of lexicographic order is sound; such as the following: A111 before B111 and C111 before C222 – Corey McDonald Apr 04 '14 at 19:50