I have a LinkedList called recordInformation that contains an object at each index. The object consists of a first name, last name, numnbr, and balance separated by spaces. So recordInformation looks like [[firstName lastName number balance][fname lastname number bal]]. Everytime I add a record to recordInformation, I sort the list based on last name. I've attempted to write a method, but when I output the list in another method, the indexes have not changed. Is compareTo the correct way to check for alphabetical order?
public void sortRecords(LinkedList list){
if (list.size()==1)
return;
Object obj = recordInformation.getLast();
String sortVar = obj.toString();
String[] varArray = sortVar.split(" ");
for (int i=0;i<list.size();i++){
Object obj1 = recordInformation.get(i);
String compare = obj1.toString();
String[] compareArray = compare.split(" ");
//varArray[1] and compareArray[1] hold the last names
if ((varArray[1].compareTo(compareArray[1]))<0){
recordInformation.add(i,obj);
recordInformation.remove(list.size()-1);
}
}
}