• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a practical way to organize a multi dimensional array by the length of the arrays contained  RSS feed

 
mark cortez
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a practical way to organize a multi dimensional array by the length of the arrays contained within?
I have
I would like them rearranged by collections[i].length.
I understand how to sort the content of the arrays but it would be helpful if I could get them in desc order first.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need to sort the collections array.
Effectively, sorting the collections array is no different to sorting the numbers in each individual array.

Can you use the Arrays.sort() method?
And you provide it with a comparator that sorts based on the length of the array object you pass it.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark cortez wrote:. . . a multi dimensional array . . .
There is no such thing in Java®. That is an array of arrays, which is better than a multidimensional array.

I would declare and initialise it in one line like this:-Please put spaces after each comma. You can format it in several lines like this if you prefer:-
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark cortez wrote:I would like them rearranged by collections.length.
I understand how to sort the content of the arrays but it would be helpful if I could get them in desc order first.

Unless you are also required to write your own sort: Follow Stefan's advice.

I know it looks like Arrays.sort(T[], Comparator<? super T>) is only good for a 1-dimensional array but, as Campbell says, ALL Java arrays are 1-dimensional, so a Comparable[][] is simply an array of Comparable[]s.

Thus, in your case, 'T' is Comparable[] - although the 'Comparable' is actually a red herring, since you don't need it to sort your outer array.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!