This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:-
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic