I am new to the idea of comparators and I am looking into ways to sort strings. In my code, below, I have made a list and sorted through it using the Collections.sort() method. Here is the code:
public class ComparatorTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
List<String> list = new ArrayList<String>();
list.addAll(Arrays.asList("Bob Stone", "Jordan Brand", "Bob Mass", "Dylan Walsh","Tom Mavis","Bob Ganley"));
System.out.println("Before sort "+ list);
Collections.sort(list);
System.out.println("After sort "+ list);
}
}
How can I modify this to sort using a given String to sort by, instead of just sorting alphabetically? For example, if I give the string BOBthen all the BOB's will move to the front of the list. I did ask a sorting question before but I misunderstood the idea of sorting and it was more of a filtering question (java sort list of strings by string value entered by a user). This question is different from my earlier question because now I am trying to actually sort and rearrange the Strings instead of filtering them.