Results 1 to 3 of 3
- 03-24-2012, 01:16 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
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
- 03-24-2012, 02:37 AM #2
Re: sorting an array based on certain criteria
Look at the Arrays class's sort() method. You can create a Comparator object for any criteria that you want.sort the songs based on different criteria.
Using your comparator object in the sort() method will sort the array in your desired order.If you don't understand my response, don't ignore it, ask a question.
- 03-24-2012, 02:58 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Sorting a String array based on a substring
By jonytek in forum New To JavaReplies: 1Last Post: 06-07-2011, 06:21 AM -
Sorting Array UI
By Brandon Seale in forum New To JavaReplies: 6Last Post: 02-18-2011, 01:50 AM -
How to change color of a cell in a Jtable based on a criteria
By RickAintree in forum New To JavaReplies: 5Last Post: 12-20-2010, 08:31 PM -
Sorting Array
By saqib15 in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:42 AM -
Jtable Sum of Sales based on criteria.
By javamula in forum JDBCReplies: 3Last Post: 10-05-2009, 07:08 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks