Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: sorting an array based on certain criteria

  1. #1
    ryleighbee is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default sorting an array based on certain criteria

    Hi there,

    My project is to create a song list using an array and then be able to sort the songs based on different criteria. I've already set up a Song class which assigns values to the fields title, writer, singer, duration, genre, and year. Now I'm writing a SongList class which creates a song array that holds a max and also should be able to sort the songs in the list. It is supposed to be able to sort by writer, duration, genre, and year using either bubble sort or selection sort. What I have right now is selection sort, but I don't understand what I need to do for these and have mostly just been trying to write something that compiles even though I'm pretty sure it won't work. Any help would be much appreciated.

    This is what I have so far for the SongList class:

    Java Code:
    public class SongList
    {
        private static final int MAX = 50;
        private Song[] songList;
        private int size;
    
        /**
         * Constructor for objects of class SongList
         */
        public SongList()
        {
            songList = new Song[MAX];
            size = 0;
        }
    
        /**
         * Adds a song into the array.
         * 
         * @param  s  size
         * @return true or false
         */
        public boolean addSong(Song s)
        {
            if(size <= MAX)
            {
                songList[size] = s;
                ++size;
                return true;
            }
            else
            {
                return false;
            }
        }
        
        /**
         * Finds a song in the array by the title.
         */
        public int findSong(String title)
        {
            int index = 0;
            boolean found = false;
            while(index < size && !found)
            {
                if(title.equals(title))
                {
                    index++;
                    found = true;
                }
            }
            return index;
        }
        
        /**
         * Swaps two songs in the array.
         */
        public void swapSong(int a, int b)
        {
            Song temp = songList[a];
            songList[a] = songList[b];
            songList[b] = temp;
        }
        
        /**
         * Sorts the songs by the writer.
         */
        public void sortByWriter()
        {
            for(int i = songList.length-1; i>0; i--)
            {
                int first = 0;
                for(int j = 1; j<=i; j++)
                {
                  if(songList[j]==(songList[first]))
                  {
                    first = j;  
                  }
                }
                Song temp = songList[first];
                songList[first] = songList[i];
                songList[i] = temp;
            }
        }
    Last edited by Norm; 03-24-2012 at 02:38 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: sorting an array based on certain criteria

    sort the songs based on different criteria.
    Look at the Arrays class's sort() method. You can create a Comparator object for any criteria that you want.
    Using your comparator object in the sort() method will sort the array in your desired order.
    sunde887 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: sorting an array based on certain criteria

    It is supposed to be able to sort ... using either bubble sort or selection sort.
    If this is a homework which requires that you don't use the infinitely preferable Arrays.sort(), you should say.

Similar Threads

  1. Sorting a String array based on a substring
    By jonytek in forum New To Java
    Replies: 1
    Last Post: 06-07-2011, 06:21 AM
  2. Sorting Array UI
    By Brandon Seale in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 01:50 AM
  3. Replies: 5
    Last Post: 12-20-2010, 08:31 PM
  4. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 03:42 AM
  5. Jtable Sum of Sales based on criteria.
    By javamula in forum JDBC
    Replies: 3
    Last Post: 10-05-2009, 07:08 PM

Posting Permissions

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