Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-06-2007, 05:13 PM
Member
 
Join Date: Nov 2007
Posts: 28
carderne is on a distinguished road
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:
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
Here's the part to alphabetize:

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
It seems to work. But it sometimes just throws up completely random songs ordering...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-06-2007, 06:46 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 112
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
can you post the code for compareTo?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-06-2007, 07:58 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,141
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-07-2007, 08:21 AM
Member
 
Join Date: Nov 2007
Posts: 28
carderne is on a distinguished road
Ok, I got my thing to work. Just had the wrong name for one of the variables.
@ShoeNinja - the compareTo() method is from the java api...

Thanks again hardwired, will try your method.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
alphabetizing parameters jjsaw5 New To Java 2 08-15-2007 06:43 PM


All times are GMT +3. The time now is 10:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org