2

I'm writing a tiny java application that will read a person's details from a txt file(tab delimited)(id, First Name, Last Name, DoB, Address)

The txt file is already sorted by id and contains around 30,000 records. My program should provide the user the choice to sort either by First Name, Last Name or Address.

What i've done already is that I made a class Person and made an arraylist of type Person to store all the records and then used JAVA's collection sort to sort it by using different comparators like this:

public static Comparator<Person> addComp = new Comparator<Person>(){
public int compare(Person P1,Person P2){
    String add1 = P1.getadd().toUpperCase();
    String add2 = P2.getadd().toUpperCase();
    return add1.compareTo(add2);
}
};

It works and all but what I wanted to know was if there was a faster algorithm to do it like in terms of running time and complexity.

Like if there was any other data structure that would be more suitable like a hash table or something

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
B.Lautus
  • 75
  • 7
  • 6
    This is the best way to go. If the data is not changing, you can precalculate the ordered lists immediately after load, so no sorting will be required on user interaction. – Tamas Hegedus Dec 26 '15 at 04:11
  • use insertion sorting, as the data is read from file, and insert it at its position. – Ashish Ani Dec 26 '15 at 04:12
  • but considering the size of the file, isn't 30,000 records too much to use insertion sort? – B.Lautus Dec 26 '15 at 04:16
  • @AshishAni check this out http://stackoverflow.com/a/753287/1043569 – Atri Dec 26 '15 at 04:18
  • @B.Lautus but if we go for merge or quick sort, we have to first load all the data and then perform the sorting. Which is time consunming – Ashish Ani Dec 26 '15 at 04:20
  • 3
    I'm voting to close this question as off-topic because this is asking about working code, it belongs on http://codereview.stackexchange.com –  Dec 26 '15 at 04:26
  • Welcome to Stack Overflow! It seems that your code currently works, and you are looking to improve it. Generally these questions are too opinionated for this site, but you might find better luck at [the Code Review Stack Exchange](http://codereview.stackexchange.com/tour). Remember to read [their requirements](http://codereview.stackexchange.com/help/on-topic) as they are a bit more strict than this site. – Kevin Brown-Silva Dec 27 '15 at 23:01

1 Answers1

2

I think this is the best way to go. Using hashtables won't help here. In terms of asymptotic time complexity, you can't get any faster in general.

However you can improve user experience by pre-ordering the data after loading from file, so you only have to change a list reference on user interaction:

private List<Person> persons_by_id;
private List<Person> persons_by_name;
private List<Person> persons_by_address;

// ...

persons_by_id = readPersonsFromFile();
persons_by_name = new ArrayList<Person>(persons_by_id);
persons_by_address = new ArrayList<Person>(persons_by_id);
Collections.sort(persons_by_name, Person.ORDER_BY_NAME);
Collections.sort(persons_by_address, Person.ORDER_BY_ADDRESS);

You can make your comparators shorter with java8:

class Person {
    // ...
    public static final Comparator<Person> ORDER_BY_NAME =
        Comparator.comparing((Person p) -> p.getName().toUpperCase());
    public static final Comparator<Person> ORDER_BY_ADDRESS =
        Comparator.comparing((Person p) -> p.getAddress().toUpperCase());
}
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97