# Sorting Arrays

• 02-12-2014, 11:38 PM
AlexGraal
Sorting Arrays
Hey.

EDIT: Sorry, these are all arraylists and not arrays

I have currently two arrays.

Code:

```String[] chunks = {"a","b","c"}; int[] freq = {2,4,3};```
I am looking for a way to order these from greatest to smallest based on frequency.

Aka, the resulting arrays should be:
Code:

```chunks = {"b","c","a"}; freq = {4,3,2};```
Because b is most frequent, and a is least frequent.

One way I can think of doing this is a two dimensional String array

Code:

`String[][] fullHold = {{"b","c","a"},{"4","3","2"}};`
Then sort based on the character values of the second row.

How can I sort a two dimensional array based on a single row (where it will rearrange the other rows in accordance).
• 02-12-2014, 11:52 PM
jim829
Re: Sorting Arrays
Assuming you know how to sort an array, just sort on the frequency. But when you do the swap, swap the corresponding array elements as well.

Regards,
Jim
• 02-12-2014, 11:53 PM
AlexGraal
Re: Sorting Arrays
Well, if it were that simply, I'd readily do that.

Correct me where I'm wrong:

If I do just a simple sort...meaning call on the built in sort, that'll only sort what I call. AKA, I don't know what is swapping. An array goes in, and an array comes out.

EDIT: These are Arraylists, sorry.
• 02-13-2014, 12:00 AM
jim829
Re: Sorting Arrays
If you don't know what swapping is then you have never implemented a sorting algorithm. So, do you have to use parallel lists or can you use a single list of a certain class?

Regards,
Jim
• 02-13-2014, 12:18 AM
AlexGraal
Re: Sorting Arrays
Yup. You're right. I haven't implemented a sorting algorithm. I know what swapping is. I've seen them and more or less understand, but I've never implemented one.

I don't have to use parallel lists. I read up on using maps and such, but that required a custom sorting algorithm as well.
• 02-13-2014, 12:27 AM
jim829
Re: Sorting Arrays
Ok, there are several possibilities here. One is to write your own sort routine. A simple bubble sort is a good one to start with but they are very inefficient. You can read up on it on the web. The other suggestion is to write a small class to hold the frequency and the chunks. Then create a new instance of that class for each array element and store it in a List<YourClass>. Then implement the Comparator<YourClass> interface and pass it along with your list to the appropriate Collections.sort() methods. Note that in both cases you only want to compare the frequency, not the chunks. But when you swap (if working with parallel lists) you need to swap both.

Regards,
Jim
• 02-13-2014, 02:42 AM
notivago
Re: Sorting Arrays
Jim sugestion about creating a class is the proper way of doing that.

I find it agravating that people so often completly disregard the language paradigm and keep on doing wire level stuff(as wire as it can be in java anyways) instead of using OO to solve the problems.

Here is the solution: https://bitbucket.org/notivago/sampl...cy/?at=default

And there is an almost identical one on linkedin that I gave this code:
https://bitbucket.org/notivago/sampl...di/?at=default

May the code be with you.