I've gotten the basics of insertion code but I'm stuck on how to compare it with another condition other then (list by A-Z, Z-A)

for the example a list of staff ranks were given :

public static final String[] staffrank = {
        "Trainee",
        "Junior",
        "Senior",
        "Administrator",
        
    };

I have a method to compare
If it returns 0 means they're of equal rank (staff 1 and staff 2)
if it returns -1 means staff 1 is lower rank than staff 2
if it returns 1 means staff 1 is higher rank than staff 2

Then I have a list of staffs in void main

Staff[] snames;
int countname=0;
snames = new Staff[50];
snames[countname++] = new Staff("Amy","Trainee");
snames[countname++] = new Staff("Annie","Junior");
snames[countname++] = new Staff("Tom","Administrator");
snames[countname++] = new Staff("Dave","Trainee");
snames[countname++] = new Staff("Gary","Junior");
snames[countname++] = new Staff("Donna","Senior);

then the insertion sort compare code

public static void insertionSortbyRank(Staff[] snames, int countname) {


//insertion sort
for(int i =1; i < countname; i++) {
int j = i;
int comparerank = Staff.compareRank(snames[j],snames[j-1]);

String name = snames.getName();
String rank = snames.getRank();

//if staff is lower rank
if(comparerank==-1) {

Then i'm unsure what to put in this while loop
since if i just do the following, it returns a A-Z / Z-A thing instead

while( j >0 && rank.compareToIgnoreCase(snames[j-1].getRank()) > 0)) {
   list[j].rank =[j-1].rank;
   list.[j].name = [j-1].name;
   
   j--;
}

then the end is replacing the new values

snames[j].name = name;
snames[j].rank = rank;

Any help would be appreciated..thank you

Edited 5 Years Ago by shiroshiro: n/a

Have you got a method that orders the rank of "Trainee", "Junior", "Senior", "Administrator" ?

It's still ordering by alphabetical order because they are not numbered in any way so it won't know the rank of these nouns.

Sorry but i don't understand what u're asking?

the compareRank returns me whether snames[1] or snames[0] is bigger / smaller and stuff..and is a must use method according to the instructions

but i can't figure out how to use that to apply it into the insertion sort ..

cause for simple cases like alphabetical order is just to check which one is bigger and wola..

This article has been dead for over six months. Start a new discussion instead.