0

Posting a revised question from my original, found at at Sorting Arrays Using Variables Within the Array Basically, I cannot seem to find a method for sorting my array based on the eventCodeString of my Event class. Because the string is alphanumeric (e.g. A123, B321, C222) the conventional Comparator (which I just discovered, thanks to the person in the original post) does not work. The more solutions I come across and try to implement, the more confused I become. Please assist me in understanding the logic of array object comparisons involving strings.

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

    public static void main(String[] args)
    {
        callMotto();
                int x, sortMethod;
                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
                do
                {
                    String sorting;

                    sorting = JOptionPane.showInputDialog("Please choose sorting method:\n"
                            + "1 to sort by event number\n"
                            + "2 to sort by number of guests\n"
                            + "3 to sort by event type\n"
                            + "Type 99 to exit sorting list");
                    sortMethod = Integer.parseInt(sorting);
                    //Event code sorting start
                    if(sortMethod == 1)
                    {   
                        for(x = 0; x < 8; ++x)
                        {

                        } 
                    }
                    //Event code sorting end
                    if(sortMethod == 2)
                    {
                        for(x = 0; x < 8; ++x)
                        {
                            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;
                                }                                   
                            });
                            eventStuff[x].largeParty();
                        }
                    }
                    //Event type sorting start
                    if(sortMethod == 3)
                    {
                        for(x = 0; x < 8; ++x)
                        {
                            Arrays.sort(eventStuff, new Comparator<Event>() 
                            {
                                @Override
                                public int compare(Event o1, Event o2) 
                                {
                                    if (o1.getEventStr() < o2.getEventStr())
                                        return -1;
                                    else if (o1.getEventStr() == o2.getEventStr())
                                        return 0;
                                    else
                                        return 1;
                                }                                   
                            });
                            eventStuff[x].largeParty();
                        }
                    //Event type sorting end
                //Sorting method end
                    }
                    if(sortMethod == 99)
                        System.exit(0);
                }
                while(sortMethod != 99);
        }   
    public static void callMotto()
    {

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

    }

}   
3
  • What kind of sort do you want, simple character value sort, or do you want, eg, "X-123" to sort after "X-13"? Commented Apr 4, 2014 at 22:38
  • I need the array sorted as follows: assuming the user enters some arbitrary values, say B111, A222, C333, C222, E555, and E111, I would like the output to be A222, B111, C222, C333, E111, E555. Commented Apr 4, 2014 at 22:46
  • That's just plain alphanumeric sort, what is the difficulty? You just have to understand that only "primitives" can be compared with > < ==, and for objects you need to use an object-specific compare method. String has such a method, named compareTo, or you can use compareToIgnoreCase if you wish. Commented Apr 4, 2014 at 22:57

1 Answer 1

2

It's simply because you can't compare Strings using ==, you should use String#equals instead. Same goes for <, <=, !=, > and >=.
You could refer to this other StackOverflow question for explanations.

That is, you have to replace:

Arrays.sort(eventStuff, new Comparator<Event>() {
    @Override
    public int compare(Event o1, Event o2) 
    {
        if (o1.getEventStr() < o2.getEventStr())
            return -1;
        else if (o1.getEventStr() == o2.getEventStr()) // this line
            return 0;
        else
            return 1;
    }                                   
});

by:

Arrays.sort(eventStuff, new Comparator<Event>() {
    @Override
    public int compare(final Event o1, final Event o2) {
        return o1.getEventStr().compareTo(o2.getEventStr));
    }                                   
});

That String#compareTo(String) method comes from the fact that Java Strings implement Comparable<String>, which basically means that they inherently know how to compare themselves.


Since I'm at it, I would suggest you to also change the implementation of that Comparator on guests numbers:

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

could become:

return o1.getGuests() - o2.getGuests;

If you think about it, it will indeed:

  • return a negative value when o1.getGuests() < o2.getGuests()
  • return 0 when o1.getGuests() == o2.getGuests()
  • return a positive value when o1.getGuests() > o2.getGuests()

... which is exactly what we need :)

Sign up to request clarification or add additional context in comments.

1 Comment

Not only have you answered my question, but you cleaned up my code, as well. Thank you for the assistance, I've been at this for 5 hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.