Results 1 to 9 of 9
Thread: List of Lists sorting
- 05-14-2011, 03:06 PM #1
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
List of Lists sorting
Hi, I am having some problems sorting this one.
Java Code:alist = [B, a, n, a, n, a, s]
after this prints: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); }
but writes in data list this: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]
It's first problem, well i believe that this is becouse of the same nameJava 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]
, but then how to do this correctly.Java Code:alist
Other problem is that i want to sort data list in alphabet order.
I would be grateful for some help.
-
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); }
- 05-14-2011, 03:38 PM #3
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
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
-
- 05-14-2011, 03:53 PM #5
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
Well now i am trying this
Don't really know how to do 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(); } });
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.
-
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.
- 05-14-2011, 04:39 PM #7
Member
- Join Date
- Mar 2010
- Location
- Lithuania
- Posts
- 22
- Rep Power
- 0
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(); } });
-
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
- 05-14-2011, 05:26 PM #9
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
You can use Column Comparator « Java Tips Weblog.Maybe you can give me some hint how i could sort listOfLists
Similar Threads
-
Need help with sorting a linked list
By SteroidalPsycho in forum New To JavaReplies: 0Last Post: 05-04-2010, 01:15 AM -
Storing Date and Sorting Lists
By Haegr in forum New To JavaReplies: 2Last Post: 04-22-2010, 09:44 PM -
list sorting problem.
By bit_bit in forum Advanced JavaReplies: 2Last Post: 02-26-2010, 04:17 AM -
List Sorting method.
By bit_bit in forum New To JavaReplies: 1Last Post: 02-24-2010, 11:44 AM -
Sorting a linked list
By Hayzam in forum New To JavaReplies: 4Last Post: 01-18-2009, 12:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks