Question: Java creating a music player, need help sorting through the songs in a playlist. based on menu selection, you can veiw the songs by title,
Java
creating a music player, need help sorting through the songs in a playlist. based on menu selection, you can veiw the songs by title, artist or release year. i have 4 classes; MyPod which is the main, Song, playlist and Menu.
i need to be able to sort throuh the playlist array to sort by title, artist or release year....
belew is the playlist class;
public class Playlist { static Song[] playlist = new Song[40]; //Constructor public Playlist() {
} public String PopulatePlaylist (String NewPlaylist) throws Exception { for(int i = 0; i < playlist.length; ++i) //loop to fill playlist array { try { playlist[i].AddSong(); //adds songs from input from Song class } catch (Exception e) { throw new Exception("Could not add song to playlist." + e.getMessage()); } }//end of for loop to fill playlis array return NewPlaylist; }//End of PopulatePlaylist() -------------------this is what i have so far for sorting by title but have problem with comparing ------------- public static void TitleSort() { // Define method // Method accepts an array to sort, changes made in this function saved in that array for( int startScan = 0; startScan < (playlist.length-1); startScan++ ) // Outer loop is the number of times we need to go through to sort it completely { int minIndex = startScan; // Reset starting point Song minValue = playlist[startScan]; // And starting minimum value for( int index = startScan + 1; index < playlist.length; index++ ) // Inner loop loops over every element left to be sorted in the array { if( playlist[index] < minValue ) // Flip sign to sort in descending order { // If we found a new low value, save it off for now minValue = playlist[index]; // both value for future comparison minIndex = index; // and index for swapping } } playlist[minIndex] = playlist[startScan]; // Swap the starting point with low spot playlist[startScan] = minValue; // And put that low value back in start } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
