Results 1 to 4 of 4

Thread: Alphabetizing

  1. #1
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default 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:
    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
    Here's the part to alphabetize:

    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
    It seems to work. But it sometimes just throws up completely random songs ordering...

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    can you post the code for compareTo?

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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;
        }
    }

  4. #4
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    7

    Default

    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.

Similar Threads

  1. alphabetizing parameters
    By jjsaw5 in forum New To Java
    Replies: 2
    Last Post: 08-15-2007, 05:43 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
  •