Results 1 to 9 of 9
  1. #1
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default List of Lists sorting

    Hi, I am having some problems sorting this one.

    Java Code:
    alist = [B, a, n, a, n, a, s]
    Java Code:
    ArrayList<Character> alist = new ArrayList<Character>();
     ArrayList<ArrayList<Character>> data = new ArrayList<ArrayList<Character>>();
    
    
           
     for(int i=0; i<list.length-1; i++){
            data.add(i, alist);
            Collections.rotate(alist, 1);   
            System.out.println(alist);
            }
    after this prints:
    Java Code:
    [s, B, a, n, a, n, a]
    [a, s, B, a, n, a, n]
    [n, a, s, B, a, n, a]
    [a, n, a, s, B, a, n]
    [n, a, n, a, s, B, a]
    [a, n, a, n, a, s, B]
    but writes in data list this:
    Java Code:
    [a, n, a, n, a, s, B]
    [a, n, a, n, a, s, B]
    [a, n, a, n, a, s, B]
    [a, n, a, n, a, s, B]
    [a, n, a, n, a, s, B]
    [a, n, a, n, a, s, B]
    It's first problem, well i believe that this is becouse of the same name
    Java Code:
    alist
    , but then how to do this correctly.

    Other problem is that i want to sort data list in alphabet order.

    I would be grateful for some help.

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

    Default

    Your List of Lists is holding multiple Lists, but they all refer to the very same object, and for this to work, you'll need to make a new List object for each item that you'll be adding into your data List of Lists. For example

    Java Code:
       public static void main(String[] args) {
          List<String> aList = Arrays.asList(new String[]{"a", "b", "c", "d", "e", "f"});
          List<List<String>> listOfLists = new ArrayList<List<String>>();
          
          List<String> newList = new ArrayList<String>(aList);
          listOfLists.add(newList);
          for (int i = 1; i < aList.size(); i++) {
             newList = new ArrayList<String>(newList);
             Collections.rotate(newList, 1);
             listOfLists.add(newList);
          }
          
          System.out.println(listOfLists);
          
       }

  3. #3
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    Thanks i see now this worked fine. Maybe you can give me some hint how i could sort listOfLists.

    I think that this one could help:

    Collections.sort(List of lists, Comparator c).

    or not?

    but how to write that Comparator that it would sort lists in normal order

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

    Default

    Quote Originally Posted by Nerijus View Post
    Thanks i see now this worked fine. Maybe you can give me some hint how i could sort listOfLists.

    I think that this one could help:

    Collections.sort(List of lists, Comparator c).

    or not?

    but how to write that Comparator that it would sort lists in normal order
    Yes, a Comparator should work fine. As to how, I don't know since what is "normal order"? Your Comparator will only need one method, compare. What have you tried so far?

  5. #5
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    Well now i am trying this
    Java Code:
    Collections.sort(lisOfLists, new Comparator<List<Character>>() {
    
                    @Override
                    public int compare(List<Character> o1, List<Character> o2) {
                       /* if(o1.get(0).compareTo(o2.get(0))==1){
                            
                        }*/
                       return o2.size() - o1.size();
                    }
    
              
    
                });
    Don't really know how to do this.
    I need that first element in lisOfLists would be aList which first char in alphabet order would be the highest.

    If there are 2 lists or more which first char is the same then it have to rely on the second char and so on till the end.

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

    Default

    Why not use a simple for loop, checking each Character in one list with the other via Character's compareTo method. If not 0, then return that value.

  7. #7
    Nerijus is offline Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    22
    Rep Power
    0

    Default

    well i don't what i am doing :D
    can you help me ?
    Java Code:
    Collections.sort(data, new Comparator<List<Character>>() {
    
                    @Override
                    public int compare(List<Character> o1, List<Character> o2) {
                        int i=0;
                        while(i!=o1.size()){
                        if(o1.get(i).compareTo(o2.get(i))==0){
                            return o1.size();
                        }
                        i++;
                        }
                        return o2.size();
                    }
    
              
    
                });

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

    Default

    I have no idea why you're trying to return the size of the list since a compare method will usually return 1, 0, or -1 and size has nothing to do with it, and also, you're also not using the for loop as I suggested. But if you think through this thing logically rather than just guessing, you should be able to solve it. I'll give you a start with pseudocode:

    Java Code:
             compare method that compares list o1 and o2 and returns and int
                since both lists are same size, for-loop through one of the lists
                   compare the i-th item of each list.
                   If the result isn't 0, return the result.
                end of for loop
                If we've made it this far, all items are the same, so return 0
             end of compare method

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

Similar Threads

  1. Need help with sorting a linked list
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-04-2010, 01:15 AM
  2. Storing Date and Sorting Lists
    By Haegr in forum New To Java
    Replies: 2
    Last Post: 04-22-2010, 09:44 PM
  3. list sorting problem.
    By bit_bit in forum Advanced Java
    Replies: 2
    Last Post: 02-26-2010, 04:17 AM
  4. List Sorting method.
    By bit_bit in forum New To Java
    Replies: 1
    Last Post: 02-24-2010, 11:44 AM
  5. Sorting a linked list
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 01-18-2009, 12:07 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
  •