Results 1 to 4 of 4
Thread: Alphabetizing
- 11-06-2007, 04:13 PM #1
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Alphabetizing
Hey all
I'm busy writing a program - as stated in an earlier post of mine - to keep a database of songs and then allow them to be listed, sorted alphabetically by track artist etc...
here are the variables:
Here's the part to alphabetize:Java Code:songDis[100][4] //nested array, 100 songs, 4 properties for each temp[] //array to temporarily store the songDis array, while swapping around songNum //the amount of songs sortBy //can be 0, 1, 2 or 3 to signify whether to sort by track artist etc
It seems to work. But it sometimes just throws up completely random songs ordering...Java Code:for (int a = 1; a < songNum; a++)//alphabetizing songs { for (int b = a + 1; b < songNum + 1; b++) { if (song[a][sortBy].compareTo(song[b][sortBy]) > 0) { temp = songDis[a]; songDis[a] = songDis[b]; songDis[b] = temp; } }//for b }//for a
- 11-06-2007, 05:46 PM #2
can you post the code for compareTo?
- 11-06-2007, 06:58 PM #3
Java Code:import java.text.*; import java.util.Date; public class SortingSongs { static DateFormat df = new SimpleDateFormat("dd MMM yyyy"); static Object[][] songs = { // track artist album date { "Blue Universe", "Craig Chaquico", "Once In A Blue Universe", getDate("01 Jun 1997") }, { "Hymn For Her", "Rick Braun", "Body And Soul", getDate("25 Jan 1997") }, { "Magic", "Ken Navarro", "Smooth Sensation", getDate("14 Oct 1997") }, { "After The Rain", "Boney James", "Sweet Thing", getDate("12 Mar 1997") } }; static int[] catSizes; public static void main(String[] args) { initCatSizes(); // Sort by category, viz, column in songs. for(int j = 0; j < songs[0].length; j++) { sort(j); print(); System.out.println("-----------------"); } } private static void sort(int cat) { int c = 0; for(int j = 0; j < songs.length; j++) { for(int k = j+1; k < songs[j].length; k++) { if(cat < 3) c = ((String)songs[j][cat]).compareTo((String)songs[k][cat]); else c = ((Date)songs[j][cat]).compareTo((Date)songs[k][cat]); if(c > 0) { Object[] temp = songs[j]; songs[j] = songs[k]; songs[k] = temp; } } } } private static void print() { for(int j = 0; j < songs.length; j++) { for(int k = 0; k < songs[j].length; k++) { String s = getString(j, k); int spaces = catSizes[k] - s.length(); System.out.print(s + space(spaces)); if(k < songs[j].length-1) System.out.print(" "); } System.out.println(); } } private static Date getDate(String s) { Date date = null; try { date = df.parse(s); } catch(ParseException e) { System.out.println("Parse error for: " + s); } return date; } private static void initCatSizes() { int numCats = songs[0].length; catSizes = new int[numCats]; for(int col = 0; col < numCats; col++) { int max = 0; for(int row = 0; row < songs.length; row++) { int length = getString(row, col).length(); if(length > max) max = length; } catSizes[col] = max; } } private static String getString(int row, int col) { if(col == 3) return df.format(songs[row][col]); return (String)songs[row][col]; } private static String space(int n) { String s = ""; for(int j = 0; j < n; j++) s += " "; return s; } }
- 11-07-2007, 07:21 AM #4
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
Similar Threads
-
alphabetizing parameters
By jjsaw5 in forum New To JavaReplies: 2Last Post: 08-15-2007, 05:43 PM


LinkBack URL
About LinkBacks

Bookmarks