# Thread: Sorting Arrays

1. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Sorting Arrays

Hey.

EDIT: Sorry, these are all arraylists and not arrays

I have currently two arrays.

Java 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:
Java 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

Java 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).
Last edited by AlexGraal; 02-12-2014 at 11:54 PM.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,647
Rep Power
9

## 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

3. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## 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.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,647
Rep Power
9

## 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

5. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## 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.

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,647
Rep Power
9

## 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

7. Heavy Coffe Drinker
Join Date
Feb 2014
Location
São Paulo, Brazil
Posts
29
Rep Power
0

## 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.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•