java - How can I sort a Linked-List based on last names? -


i have program requests first , last names of 10 people console , adds them linked list under single entity, 'fullname'. there way use collections.sort() sort names based on last name , not first. order of first name last name needs kept. have far:

public void requestnames(){          for(int = 1; < 11; i++){              // request first name.             system.out.print("enter first name of friend # " + + ":");             string fname = scanner.nextline();              // request last name.             system.out.print("enter last name of friend # " + + ":");             string lname = scanner.nextline();              // concatenate first , last name , hold in fullname variable.             string fullname = fname + " " + lname;              mylist.add(fullname);         }         scanner.close();     }      public void sortlist(){         collections.sort(mylist);     } 

yes. provided using linkedlist java collections api, can write custom comparator operation.

collections.sort(mylist, new comparator<string, string>() {     public int compare(string left, string right) {         string leftlast = string.split("\\s")[1];         string rightlast = string.split("\\s")[1];         return leftlast.compareto(rightlast);     } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -