2

I have an Class "Person" which contains a constructor with 3 parameters. Now i want a sorting option, i need it to sort by ID, Name, or Address ascending and descending

public class Person
{
private int id;
private String name;
private String addr;


public Person()
{
    id = MyTools.getInteger("ID: ");
    name = MyTools.getString("Name: ");
    addr = MyTools.getString("Address: ");

}

and here is where i want to sort it:

public static void main(String[] args) throws FileNotFoundException
{
    boolean loop = true;

    System.out.println("Choose:");
    System.out.println("\t" + "0 = Exit programm");
    System.out.println("\t" + "1 = Add new");
    System.out.println("\t" + "2 = Show");
    System.out.println("\t" + "3 = Sort");
    System.out.println("\t" + "4 = Delete");
    System.out.println("\t" + "5 = Save");
    System.out.println("\t" + "6 = Help");

    TextFile tf = new TextFile("AdressVerwaltung.txt", 'o');
    List<Person> adressen = new ArrayList<Person>();
    int listSize = 0;
    while (loop)
    {

int choice = MyTools.getInteger("Please choose which operation you want 
to execute (6 = Help): "+"\n");

    switch (choice)
    {
        case 0:
        System.out.println("Programmende");         
        for (int k = 0; k < listSize; k++)
        {
            tf.printLine(adressen.get(k).toString());
        }
        tf.close();
        loop = false;
        break;

      case 1:


  Person p = new Person();
  adressen.add(p);
  System.out.println("Data is put in Array!");
  listSize = adressen.size();

break;
case 2:

break;
case 3: 
break;

Now i want to be able to choose if i want to sort by ID, by name or by address

sidgate
  • 14,650
  • 11
  • 68
  • 119

1 Answers1

1

You can use the Comparator interface in Java to create some custom comparators:

For example a User object:

public class User {
    private final String username;
    private final Integer age;

    public User(String username, Integer age) {
        this.username = username;
        this.age = age;
    }

    public String getUsername() {
        return username;
    }

    public Integer getAge() {
        return age;
    }
 }

An example comparator which sorts by username:

public class UsernameComparator implements Comparator<User> {
    @Override
    public int compare(User user, User user2) {
        return user.getUsername().compareTo(user2.getUsername());
    }
}

and to use:

    List<User> users = asList(
        new User("Michael", 34),
        new User("John", 30),
        new User("James", 30),
        new User("Mark", 29));

UsernameComparator userNameComparator = new UsernameComparator();
users.sort(userAgeComparator);

OR using Collections.sort(.....):

Collections.sort(users, userAgeComparator);

OR using java 8's streams API you can avoid creating a new Comparator class and just use streams and the sorted method:

    List sortedList = users.stream()
            .sorted(Comparator.comparing(User::getUsername))
            .collect(Collectors.toList());

Good Luck :-)

Michael W
  • 3,515
  • 8
  • 39
  • 62