7

To avoid the duplicate claims, I've looked at this post and it isn't exactly what I wanted.
Every other 2D ArrayList question involved double or int numbers; my question is about Strings.

What I'm doing

I have a 2D ArrayList, defined like this:

ArrayList<ArrayList<String>> namesAndNumbers = new ArrayList<ArrayList<String>>();

The idea is that the first item in every row of ArrayLists contains the names, and the rest of the columns in each row contains the phone numbers (unknown amount). Therefore I would like to avoid converting it to a regular array.

Sample

Let's say I've populated my ArrayList and I have this:

{"Mike", "(805) 766-4920"}
{"Emily", "(705) 668-9292", "(705) 555-1060"}
{"James", "(605) 965-2000"}

I expect my output to be this:

{"Emily", "(705) 668-9292", "(705) 555-1060"}    
{"James", "(605) 965-2000"}
{"Mike", "(805) 766-4920"}

I want to keep the numbers corresponding with the names, but simply sort the array by the name.

What answers I hope for

I'm hoping for a built-in function manipulation type of thing, but I'd be fine if someone created a sorting array method for me with an 2D ArrayList as the input. I haven't seen any question that answers this issue explicitly. I will continue trying to come up with an answer myself as well.

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • Why don't you use Collections in Java for this - i think that would be a perfect tool for your case, isn't it? – VJ Vélan Solutions Dec 09 '13 at 21:07
  • @harikris it is a nested ArrayList. Yes, I think Collections can work but I have no idea what to do with it. – Michael Yaworski Dec 09 '13 at 21:08
  • 1
    If you used Collections, then sorting would become a breeze. Also, instead of Lists, if you used Sets, then you can avoid duplicate entries as well - all done automatically for you. It's not difficult to learn about Collections and Generics. May i suggest a reference book? Not sure if that will come across as promotional. I did not write this book. Just think it's an excellent resource. Authors are Kathy Sierra and Bert Bates. They have a chapter on Collections/Generics in their Head First Java book. HTH. – VJ Vélan Solutions Dec 09 '13 at 21:14
  • @harikris You can suggest it, but I probably won't read it to answer this question. I don't have the time currently to read it immediately. Possibly later, though. Thanks. – Michael Yaworski Dec 09 '13 at 21:15
  • No problem. There's a working example in that chapter. Just so you know. For later use. – VJ Vélan Solutions Dec 09 '13 at 21:18
  • You should read and follow the accepted answer in that question, it still applies to your question, even though it's not an exact duplicate! – Robin Green Dec 09 '13 at 21:19
  • @RobinGreen I am going to try and follow that, but I would also like an answer on this question without going to the effort of creating another class. – Michael Yaworski Dec 09 '13 at 21:22

3 Answers3

18

You can use Collections.sort and provide a custom Comparator where you compare the first element of each list, i.e the name :

List<ArrayList<String>> namesAndNumbers = new ArrayList<ArrayList<String>>();
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("Mike", "(805) 766-4920")));
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("Emily", "(705) 668-9292", "(705) 555-1060")));
namesAndNumbers.add(new ArrayList<String>(Arrays.asList("James", "(605) 965-2000")));
Collections.sort(namesAndNumbers, new Comparator<ArrayList<String>>() {    
        @Override
        public int compare(ArrayList<String> o1, ArrayList<String> o2) {
            return o1.get(0).compareTo(o2.get(0));
        }               
});
System.out.println(namesAndNumbers);

Output :

[[Emily, (705) 668-9292, (705) 555-1060], [James, (605) 965-2000], [Mike, (805) 766-4920]]
user2336315
  • 15,697
  • 10
  • 46
  • 64
5

Create a custom comparator:

final Comparator<List<String>> comparator = new Comparator<List<String>>() {
    public int compare(List<String> pList1, List<String> pList2) {
        return pList1.get(0).compareTo(pList2.get(0));
    }
};
final List<List<String>> lists = Arrays.asList(
    Arrays.asList("Mike", "(805) 766-4920"),
    Arrays.asList("Emily", "(705) 668-9292", "(705) 555-1060"),
    Arrays.asList("James", "(605) 965-2000")
);
Collections.sort(lists, comparator);
for (List<String> list : lists) System.out.println(list);
Hollis Waite
  • 1,079
  • 1
  • 20
  • 31
1

For diversity Why dont you just use a HashMap unless you want duplicate keys. the key would be name and the value would be an ArrayList of numbers

String name="John";

ArrayList<String> phonenumbers=new ArrayList<String>();

phonenumbers.add("80925887");
phonenumbers.add("+166476654");

TreeMap<String, ArrayList> tm=new TreeMap<String, ArrayList>();

tm.put(name, phonenumbers);
Yehia Awad
  • 2,898
  • 1
  • 20
  • 31
  • This could be a good suggestion (I've never used HashMaps before), but this isn't an answer to my question. If you could, supplement this answer with a way to sort that HashMap. – Michael Yaworski Dec 09 '13 at 21:26
  • Definitely buddy if your purpose is natural order you can use TreeMap instead of HashMap they both implements Map interface – Yehia Awad Dec 09 '13 at 21:28