10 November 2008

Bubblesort Multidimensional Array in Java

This quick work around will allow for an easy an "efficient" sort of a multidimensional array in Java. Simply pass your array in and the index you'd like to sort on.
Although there are other solutions to this, using java's builtin Arrays.sort method won't cut it. I put together this code to be called as a private method within your application. Enjoy!

This works great when using a String[][] compare. If you are trying to sort integers, simply change your array and take out the if (...compareTo...) business and just check which is greater.
private String[][] bubbleSortMulti(String[][] MultiIn, int compIdx) { String[][] temp = new String[MultiIn.length][MultiIn[0].length]; boolean finished = false; while (!finished) { finished = true; for (int i = 0; i < MultiIn.length - 1; i++) { if (MultiIn[i][compIdx].compareToIgnoreCase(MultiIn[i + 1][compIdx]) > 0) { for (int j = 0; j < MultiIn[i].length; j++) { temp[i][j] = MultiIn[i][j]; MultiIn[i][j] = MultiIn[i + 1][j]; MultiIn[i + 1][j] = temp[i][j]; } finished = false; } } } return MultiIn; }

12 comments:

Anonymous said...

Thanks Garrett. This worked great.

Anonymous said...

hello
give me an example to store 5 applicants name, address, position, phone in a array also to disply the array values.

garrett wyrwas said...

There's plenty of info about this on the internet. Also see my post @ http://realityisimportant.blogspot.com/2008/06/how-to-read-and-write-multi-dimensional.html

That will give you some array info at least for a multi-dim array.

What you're looking for can be done a couple of different ways. If you're doing it through a 2 dimensional array then...
String[][] Applicants = new String[5][4];
String[0][0] = "A1Name";
String[0][1] = "A1Addy";
...
String[1][0] = "A2Name";
etc...

That's a really verbose way of explaining it, not necessarily practical.

Xihad said...

hi, i m just a beginner in java. can u plz tell me what exactly needs to be done in order to make it working for int arrays??

thnx for this great example...

garrett wyrwas said...

@Xihad If you need an int array just pass in a multidimensional int array. int[][] multiIn.

then make your temp an int array also.

for the compare, instead of "MultiIn[i][compIdx].compareToIgnoreCase(MultiIn[i + 1][compIdx]) > 0"

you would do something like

if (MultiIn[i][compIdx] > MultiIn[i+1][compIdx]) ...

That would sort by greater, you can flip the sign to sort in descending order.

If you're that new to java are you sure multidimensional arrays are the way to go for whatever you're working on?

Wylder said...

Garrett,

I am new to multidimensional arrays in Java, and I am trying to sort an array of jagged dimensions. Is there anything that I should be aware of, which could be missed in the array of 1700 lines? is it possible that I may need to re dimension a line that has more elements than the one it is replacing? Would I re dimension a line that had less elements than the one that it is replacing to remove unneeded elements, as to keep out orphan elements?

Thanks,
WylderMIS

David Severns said...

Ok, I have the class that does the sort. Seems straight, but how do you call it. I understand that you provide the array, and column. But keep getting errors:

itm is array: 0-4(product, productID,cost, items_onhand.

understand it should be Call
bubbleSortMulti (itm[],[0]). Help.

vrush said...

gr8 work.

Grab Bag Chronicles said...

You are my hero! Thank you SO much!

Anonymous said...

Just wondering, what does the compLdx refer to?
Can I just create a new variable within the class for that? initialize it as zero?
Thanks,

Anonymous said...

This works great! Thanks.

-- beginner in Chicago

Anonymous said...

thanks, this was exactly what I was looking for.

In case anyone else doesn't understand what compLdx is for, it is the index of element by which array is sorted. So String MultiIn[][] = {{"c", "k"}, {"a", "m"}, {"b", "l"}};
bubbleSortMulti(MultiIn, 0) would return {{a, m}, {b, l}, {c, k}} and
MultiIn = bubbleSortMulti(MultiIn, 1) would return {{c, k}, {b, l}, {a, m}}