how to input values into ArrayList alphabetically

Urmzd :

I'm given the task of adding a value into an ArrayList alphabetically (by lastname first, and firstname if lastnames are the same),I'm not allowed to sort the ArrayList once all the inputs are given. What I am suppose to do is determine the location of where the value should be each time an input is given and place it there. Example values: Inputs are {John Doe, Bryan Sully, Harry Ache, Ali Doe, Bry Dyre} , Output should be {Harry Ache, Ali Doe, John Doe, Bry Dyre, Bryan Sully}. However, in cases where there are two of the same last names, what will occur is {Harry Ache, Ali Doe, Bry Dyre, John Doe, Bryan Sully}, why?

ArrayList<Person> list = new ArrayList<Person>();

public void addPerson(Person p){
    if(list.size() == 0){
      list.add(p);
    } else{
      for(Person pp: list){
        int getIndex = list.indexOf(pp);
        int lOrder = p.getLastName().compareTo(pp.getLastName());
        if(lOrder > 0){
          list.add(getIndex + 1, p);
          break;
        } else if(lOrder == 0){
          int fOrder = p.getFirstName().compareTo(pp.getFirstName());
          if(fOrder > 0){
            list.add(getIndex, p);
            break;
          } else {
            list.add(getIndex + 1, p);
            break;
          }
        } else {
          list.add(getIndex, p);
          break;
        }
      }
    }
  }
D. Pereira :

One issue with your code is that you are making an insertion after a single iteration of your loop, inserting Person p only in comparison to the first Person in the list.

Here is an improved implementation:

public void addPerson(Person person) {
    if (list.isEmpty()) {
        list.add(p);
    } else {
        // the position where you will insert the Person;
        // default value is list.size(), so it can be added to the end
        int position = list.size();

        Person p;
        for (int i = 0; i < list.size(); i++) {
            p = list.get(i);

            // if the comparison result is > 0, you found the first Person
            // in the list that comes alphabetically after;
            // Insert person before it, at position i
            if (compare(p, person) > 0) {
                position = i;
                break; // only break now that you've found the insert position
            }
        }
        list.add(person, position);
    }
}

/**
 * @return 0 if both Persons p1 & p2 have the same names, 
 *         a positive number if p1 is lexicographically greater than p2, 
 *         or a negative number if p1 is lexicographically less than p2 
 */
private int compare(Person p1, Person p2) {
    int result = p1.getLastName().compareTo(p2.getLastName());
    if (result == 0) {
        result = p1.getFirstName().compareTo(p2.getFirstName());
    }
    return result;
}

As others suggested, implementing the Comparable interface in Person if possible can also help create a cleaner implementation.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=167256&siteId=1