dimanche 11 mai 2014

Java - comparant alphanumérique chaînes du tableau d'objets - Stack Overflow


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

}

}



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



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

}

}


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


0 commentaires:

Enregistrer un commentaire