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