java - sorting and searching an arraylist -


i've looked @ posts on here sorting , searching can't seem arraylist sort or search using

collections.sort(myarraylist); 

the data stored in arraylist stored when user enters data in textfields in gui. have used gettext store data (that user enters jtextfields) in arraylist.

the user should able enter firstname or surname a

string searchstring = joptionpane.showinputdialog("enter name"); 

the sort should done after user adds each new contact arraylist surname.

so question is:

  1. how can sort arraylist?
  2. how can search arraylist?

please can advise me how proceed stuck!!

the arraylist myarraylist holding contacts, assume has @ least 2 string fields (first , last name). if comparing 2 contacts, how decide 1 goes first on sorted list?

my guess have sort of contact type holding of these fields.

what need implement comparable interface contact type, sorting algorithm can figure out of 2 given contacts comes first in list.

where declare contact type, need add 2 things: implements comparable declaration , definition int compareto(object) method.

so, contact class this:

 public class contact implements comparable{       private string firstname, lastname;       public int compareto(object o){             return lastname.compareto(((contact)o).getlastname());       }       public string getlastname(){             return lastname;       }       //the rest of contact class stuff here.  } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -