Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Tolls

Thread: Sort an ArrayList of an ArrayList

  1. #1
    lakepacific is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default Sort an ArrayList of an ArrayList

    This morning I came across this following code from an old post to this forum:

    Original thread can be found here: adding values from file to multi-dimensional ArrayList

    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();
    data.add(new ArrayList<String>());
    data.get(0).add("String");

    This code does exactly what I want; a two dimensional array.

    Looks something like this:

    [0][0] = ABC
    [0][1] = ABC
    [0][2] = AJ ABC
    [0][3] = 1.23
    [0][4] = 1.23
    [0][5] = 09/26/2011

    [1][0] = ABC
    [1][1] = ABC
    [1][2] = AJ ABC
    [1][3] = 1.98
    [1][4] = 1.98
    [1][5] = 01/26/2010


    [2][0] = ABC
    [2][1] = ABC
    [2][2] = AC ABC
    [2][3] = 1.98
    [2][4] = 1.98
    [2][5] = 09/26/2011

    [3][0] ...

    However, I now have my two dimensional ArrayList (or ArrayList of ArrayList) and would like to sort it on [i,2] (type string/and my primary sort) and then on [i,5] (type string/and secondary sort that I would need to convert to a date for it to properly sort). Any suggestions on how to sort an ArrayList of ArrayList would be greatly appreciated.

    Regards,
    Joe

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Sort an ArrayList of an ArrayList

    This looks like a very bad plan as it appears you are mixing types in your array. Instead I suggest that you create a class that holds 3 Strings, 2 doubles and one Date, make this class implement the generic Comparable interface, create non-nested generic ArrayList of objects of this class and then sort it via java.util.Arrays.sort(...).

  3. #3
    lakepacific is offline Member
    Join Date
    Jun 2009
    Posts
    3
    Rep Power
    0

    Default Re: Sort an ArrayList of an ArrayList

    Thank you for the reply...coming from a VFP/VB6 background, I'll need time to digest your suggestion. The primary end result is the creation of a flat file (csv), so I believe it is okay to leave the first five elements as type string. The sixth element I was hoping to create a comparator that would convert it to a date, sort and then reconvert back to a string.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Sort an ArrayList of an ArrayList

    Even if all are Strings, what you have here is the parallel array anti-pattern, and you're still better off creating a class to hold the data that should naturally be clustered together.

    Much luck.
    Tolls likes this.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: Sort an ArrayList of an ArrayList

    As Fubarable says, this should be a class, and you'd therefore have an ArrayList<YourClass>, and you could then either make it Comparable or provide a Comparator to do the sorting. Your class would also use proper types, so the date String would actually be a Date object, and those decimals would be either doubles or BigDecimals (depending). When you stream it to your csv you would then format the non-String values correctly.

    Storing everything in Strings probably has an anti-pattern named after it...at least I would hope so, since I see it all the time in database work and it is evil.
    Fubarable likes this.

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. Sort an ArrayList
    By überfuzz in forum New To Java
    Replies: 13
    Last Post: 02-10-2011, 02:18 PM
  3. ArrayList sort
    By Dipke in forum New To Java
    Replies: 1
    Last Post: 08-23-2010, 02:01 PM
  4. how to sort an arraylist?
    By zhangster in forum New To Java
    Replies: 5
    Last Post: 03-19-2010, 09:01 AM
  5. Using Merge Sort to sort an ArrayList of Strings
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 03-13-2009, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •