Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By notivago

Thread: Sorting Arrays

  1. #1
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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 10:54 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    AlexGraal is offline Señor Member
    Join Date
    Jan 2014
    Posts
    184
    Rep Power
    0

    Default 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. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default 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
    AlexGraal likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default 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.
    AlexGraal likes this.

Similar Threads

  1. Sorting arrays
    By Blondedude092 in forum New To Java
    Replies: 2
    Last Post: 12-01-2012, 01:57 AM
  2. Sorting arrays
    By Lex in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 02:19 AM
  3. Help with sorting arrays
    By Joycey in forum New To Java
    Replies: 4
    Last Post: 03-30-2010, 08:35 PM
  4. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  5. Sorting Two Arrays
    By Faye Rett in forum New To Java
    Replies: 4
    Last Post: 03-07-2010, 01:00 AM

Posting Permissions

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