I've gotten the basics of insertion code but I'm stuck on how to compare it with another condition other then (list by AZ, ZA)
for the example a list of staff ranks were given :
public static final String[] staffrank = {
"Trainee",
"Junior",
"Senior",
"Administrator"
};
I have a method to compareRank
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 still giving me an unsorted list
while( j >0 && rank.compareRank(list[j], list[j - 1]) == 1))) {
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;
the output suppose to give : (by order of their ranks from low to highest according to the chart)
Amy, Trainee Dave, Trainee Annie, Junior Gary, Junior Donna, Senior Tom, Administrator
Any help would be appreciated..thank you
I would assume that since you've been given a staffrank
variable, that you should probably make use of it. However, aside from mentioning it at the top of your question, you make no other use of it that we've been shown...So my assumption is that you've ignored it completely.
I think that's the key to your puzzle here.
Note that I'm ignoring the fact that String arrays are not the best representation (I'd go for an enum
or something like a class...Potentially implementing Comparable<StaffRank>
somewhere...But this is a test question as noted, after all...)
You could make a function like this:
public static final int compareRank(String[] ranks, String rank) {
for (int i=0; i<ranks.length; i++) {
String string = ranks[i];
if string.equals(rank) return i;
}
return -1;
}
Which will return an integer between -1 and (ranks.length -1) which you can use for comparison. The smaller the number the more junior the rank.
Inside your while loop you'll have to compare each staff and swap them if the "i"-th staff is greater than the "i+1"-th staff.
for (int i=0; i<(snames.length-1); i++) {
Staff sname1 = sname[i];
Staff sname2 = sname[i+1]
//compare sname1 to sname2 and swap if sname1 > sname2
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.