6 Replies - 2871 Views - Last Post: 08 January 2012 - 04:20 PM Rate Topic: -----

#1 Bladescope  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 71
  • Joined: 01-September 09

Question: Best method to order a multidimensional ArrayList?

Posted 08 January 2012 - 04:55 AM

Hey coders,

I've got a dataset in a multidimensional ArrayList and I would like the list to be ordered by the first value in each row (and integer).
I tried being a bit silly and inserting each row in the arraylist at the index of the integer of the row, but that obviously gives an outofboundsexception on runtime.
The top level array is an ArrayList<ArrayList>, with each sub array being ArrayList<Object>.

I've seemed to search quite a fair amount and people seem to like overriding the sort methods, but I'm still quite a newbie at Java and would like first hand advice.

Any help you can provide would be greatly appreciated!

-Bladescope

Is This A Good Question/Topic? 0
  • +

Replies To: Question: Best method to order a multidimensional ArrayList?

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,644
  • Joined: 20-September 08

Re: Question: Best method to order a multidimensional ArrayList?

Posted 08 January 2012 - 05:08 AM

Try


	Collections.sort(dataset, new Comparator<List<Object>> () {
	    public int compare(List<Object> list1, List<Object> list2) {
		return ((Integer)list1.get(0)).compareTo((Integer)list2.get(0));
	    }
	});


Was This Post Helpful? 1
  • +
  • -

#3 Bladescope  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 71
  • Joined: 01-September 09

Re: Question: Best method to order a multidimensional ArrayList?

Posted 08 January 2012 - 05:45 AM

Compile time error:

Quote

no suitable method found for sort(java.util.ArayList<java.util.ArrayList>, <anonymous java.util.Comparator<java.util.List<java.lang.Object>>>)
method java.util.Collections.<T>sort(java.util.List<T>,java.util.Comparator<? super T>) is not applicable
(no instance(s)) of type variable(s) T exist so that argument type <anonymous java.util.Comparator<java.util.List<java.lang.Object>>> conforms to formal parameter type java.util.Comparator<? super T>)
method java.util.Collections.<T>sort(java.util.List<T>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)


D:

All that I did was I changed 'dataset' with my ArrayList<ArrayList>

This post has been edited by Bladescope: 08 January 2012 - 05:50 AM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,644
  • Joined: 20-September 08

Re: Question: Best method to order a multidimensional ArrayList?

Posted 08 January 2012 - 06:15 AM

Please post your existing code
Was This Post Helpful? 0
  • +
  • -

#5 Bladescope  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 71
  • Joined: 01-September 09

Re: Question: Best method to order a multidimensional ArrayList?

Posted 08 January 2012 - 07:57 AM

Structure of arraylist:
ArrayList<ArrayList>
Each index contains:
ArrayList<Object>
Index as follows:
0: Integer (one I want it sorted by)
1: String
2: Boolean
3: Integer
4: Boolean
5: String
6: Integer
7: Integer

It should be noted that, I think when you add the objects to the arraylist, they all become treated as type Object, as I've had to recast them when I've needed to pass them through different methods.

Here's a method which uses the sort code:

    public ArrayList<ArrayList> getRoomList() {
        ArrayList<ArrayList> rows = new ArrayList<ArrayList>();
        
        // Loop through records
        for(int i=0;i<hostelRooms.size();i++) {
            Room hostelRoom = hostelRooms.get(i);

            // Add to Arraylist
            rows.add(createTableRow(hostelRoom));
        }
        
        // Return it.
        return rows;
    }



createTableRow() takes an Object (of type Room, out of scope) and places the data needed into an ArrayList<Object>

To add your snippet into the code, I would assume it would be inserted after the loop, before it is returned.

Something like this:
    public ArrayList<ArrayList> getRoomList() {
        ArrayList<ArrayList> rows = new ArrayList<ArrayList>();
        
        // Loop through records
        for(int i=0;i<hostelRooms.size();i++) {
            Room hostelRoom = hostelRooms.get(i);

            // Add to Arraylist
            rows.add(createTableRow(hostelRoom));
        }
        
        Collections.sort(rows, new Comparator<List<Object>> () {
            public int compare(List<Object> list1, List<Object> list2) {
	        return ((Integer)list1.get(0)).compareTo((Integer)list2.get(0));
            }
        });

        // Return it.
        return rows;
    }


Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,644
  • Joined: 20-September 08

Re: Question: Best method to order a multidimensional ArrayList?

Posted 08 January 2012 - 09:35 AM

You need to be working, not with ArrayList<ArrayList>, but ArrayList<ArrayList<Object>>

You'll find the method i gave you works then
Was This Post Helpful? 1
  • +
  • -

#7 Bladescope  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 71
  • Joined: 01-September 09

Re: Question: Best method to order a multidimensional ArrayList?

Posted 08 January 2012 - 04:20 PM

View Postg00se, on 08 January 2012 - 09:35 AM, said:

You need to be working, not with ArrayList<ArrayList>, but ArrayList<ArrayList<Object>>

You'll find the method i gave you works then


Very much appreciated for the help g00se, thank you very much. The problem has indeed been solved, and the rest of my code has been changed to reflect the correct syntax for the multidimensional array (which has, in turn, fixed a whole host of other errors).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1