Results 1 to 7 of 7
- 09-24-2012, 05:19 PM #1
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
Sorting 2-dimentional array has different size
Hello,
I am looking for a fast way to sort a 2-dimentional array with different size as per its row size in descending order.
EX:
A = {(5,9,3), (5,9,3,4),(5,9,3,4,2)}
After sorting should be
A= {(5,9,3,4,2), (5,9,3,4), (5,9,3)}
because A[2].length > A[1].length > A[0].length
Thanks.Last edited by asa; 09-25-2012 at 04:28 AM.
- 09-24-2012, 05:27 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Sorting 2-dimentional array has different size
Arrays.sort with a custom comparator which compares the lenght of two arrays?
- 09-25-2012, 01:37 AM #3
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
Re: Sorting 2-dimentional array has different size
Thanks, but how can I do that?
- 09-25-2012, 06:48 AM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Sorting 2-dimentional array has different size
I hope I haven't done your homework....Java Code:Arrays.sort(yourArray, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return Integer.valueOf(o2.length).compareTo(o1.length); } });
- 09-25-2012, 07:23 AM #5
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
Re: Sorting 2-dimentional array has different size
Thanks, but when I couldn't do it by this way.
sort takes T[] as a first parameter.
So it doesn't accept my array. My array is a string array not an int, will it work on this way?
Thanks.
- 09-25-2012, 08:38 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Sorting 2-dimentional array has different size
So, what happened when you tried? Post your code and if there were compiler messages you can't understand post them as well. If your code runs but doesn't produce the output you described, say what it does do at runtime.but when I couldn't do it by this way.
It might have been a bit misleading to use 5, 9, 3, ... as example when you're working with strings. But yes, writing a comparator as in #4 but using String[] rather than int[] is the way to go.My array is a string array not an int, will it work on this way?
- 09-26-2012, 01:30 AM #7
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
How can I format this multi dimentional array?
By EscSequenceAlpha in forum New To JavaReplies: 3Last Post: 05-14-2012, 03:50 AM -
how to read a text delimited file using 2 dimentional array in java ??
By pooja123 in forum New To JavaReplies: 2Last Post: 04-01-2011, 03:04 PM -
Sorting Array UI
By Brandon Seale in forum New To JavaReplies: 6Last Post: 02-18-2011, 01:50 AM -
Sorting Array
By saqib15 in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:42 AM -
adding elemnts of two dimentional array into vector
By sara12345 in forum New To JavaReplies: 10Last Post: 12-31-2009, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks