Sorting Arrays Using Variables Within the Array

2019-09-17 06:14发布

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"
                        +   "*******************************************************");

    }

    }   

2条回答
Emotional °昔
2楼-- · 2019-09-17 06:50

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.

查看更多
beautiful°
3楼-- · 2019-09-17 06:59

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

查看更多
登录 后发表回答