java - Using compareTo in ArrayLists -


i need using compareto in arraylists. guide given paragraph:

addorder(arraylist words, string newword) – method adds newword in proper alphabetical order.

hint can use: x.compareto(y)

if x.compareto(y) < 0, x comes before y)

addorder should return updated arraylist of strings.

but though researched alot still struggling understand how apply arraylists. code want use compareto method:

import java.util.arraylist; import java.util.arraylist; import java.util.list;  class stringlist {     public static void main(string [] args)     {         arraylist< string> list = new arraylist< string>();         list.add("goldsmiths");         list.add("college");         list.add("london");         list.add("manchester");         list.add("london");         list.add("york");         system.out.println("these words:");         system.out.println(list);         system.out.println("this size of array:");         system.out.println(lengtharray(list));         system.out.println("the length of strings is:");         system.out.println(totallengtharray(list));     }      public static int lengtharray(list<string> list)     {         return list.size();     }      public static int totallengtharray(list<string> list)     {         int count = 0;         (string s: list)         {             count += s.length();         }         return count;     } } 

i want compare , sort out list items have added can see in code.

use collections.sort(list<t> list,comarator c);

collections.sort(yourlist,new comparator<string>() {     @override     public int compare(string lhs, string rhs) {         return lhs.compareto(rhs);   //or whatever sorting algorithm     } }); 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -