java - How to use compareToIgnoreCase -
i given example on how alphabetically sort actor objects in array.
public class alphasortingexchange { public static void main(string[ ] args) { string[ ] names = {"joe", "slim", "ed", "george"}; sortstringexchange (names); ( int k = 0; k < 4; k++ ) system.out.println( names [ k ] ); } public static void sortstringexchange( string x [ ] ) { int i, j; string temp; ( = 0; < x.length - 1; i++ ) { ( j = + 1; j < x.length; j++ ) { if ( x [ ].comparetoignorecase( x [ j ] ) > 0 ) { // ascending sort temp = x [ ]; x [ ] = x [ j ]; // swapping x [ j ] = temp; } } } } }
i allowed follow sort of format in sorting array. netbeans not liking "comparetoignorecase" statement in code, giving error
"cannot find symbol: method comparetoignorecase(actors) location class actors"
. below sorting function.
public static void sortactors(actors actors[]) { int i, j; actors temp; (i = 0; < actors.length - 1; i++) { (j = + 1; j < actors.length; j++) { if (actors[i].comparetoignorecase(actors[j]) > 0) { temp = actors[i]; actors[i] = actors[j]; actors[j] = temp; } } } }
this object array , example of object in array. said before, can use comparetoignorecase. @ loss @ how use function
private static void createactorslist() { actors[] actors = new actors[constants.number_of_actors]; actors ladyviolet = new actors(); ladyviolet.setname("lady violet"); ladyviolet.setdialogue("dialogue"); ladyviolet.sethappiness(0); ladyviolet.sethealth(100); actors[constants.violet] = ladyviolet; }
any or solution appreciated!
thanks in advance!
your actor
class doesn't have comparetoignorecase
method. mean call method on 1 of class's fields, e.g.,
if (actors[i].getname().comparetoignorecase(actors[j].getname()) > 0)
if method needs on actor
class, you'd have write own implementation:
public int comparetoignorecase(actor actor) { return this.name.comparetoignorecase(actor.name); }
Comments
Post a Comment