Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help with sorting...(comparator)

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default help with sorting...(comparator)

    i have a arraylist of file objects and a class of Id3Tags that get from files a couple of strings
    i want to sort this list alphabeticly so i thought about two ways i think i can do
    the first is
    convert my arraylist<file> to an array<Id3Tag>\arraylist<Id3Tag> then create a comparator<Id3Tag>
    that gets two Id3Tags and compares Id3Tags.getArtist() for the sorting i'll use
    arrays.sort(<Id3Tag>a, new camparator)

    the second option
    is to stay with the arraylist<file> and create a comparator<file> that in it well convert both files to
    Id3Tag then compare the Id3Tag.getArtist


    well i started with the second option here is my comparator:

    iimport java.util.*;
    import java.io.*;
     
     
    public  class Mp3Comparator implements Comparator<File>
    {
    public  int compare(File mp3File1, File mp3File2)
      {
     
        Id3Tag tag1 = new Id3Tag(mp3File1);
        Id3Tag tag2 = new Id3Tag(mp3File2);
        int result;
        result = tag1.getArtist().compareTo(tag2.getArtist());
        return result;
      }
    }

    and here is my main:

    import java.io.*;
    import java.util.*;
     
    public class DemoGeter
    {
    public static void main(String[] args)
       {
     
        File demoFile = new File("M:/iPod_Control/Music");   
     
        FileGeter dir1 = new FileGeter();    //creates my fileGeter class that can get files from directory's  
     
        dir1.fileGet(demoFile); // get files from directory's 
     
        Id3Tag[] fileTags = new Id3Tag[dir1.bFiles.size()]; 
     
        Collections.sort(dir1.bFiles, new Mp3Comparator());   //bFile is the arraylist<file> in my fileGeter class
     
        if (!dir1.bFiles.isEmpty() && demoFile.exists() ){
        for (int h = 0 ; h < 20; h++)
          {
           fileTags[h] = new Id3Tag(dir1.bFiles.get(h));
           if (fileTags[h].getArtist()!=null){
           System.out.println(fileTags[h].getArtist());
                                              }
          }
     
                                                        }
        }
     }


    if someone has the time to tell me what is wrong i'll be happy thanks....
    Last edited by mdstrauss; July 25th, 2009 at 08:16 PM.

  2. #2
    Junior Member
    Join Date
    Jul 2009
    Posts
    27
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: help with sorting...(comparator)

    never mind i got it my problem was that some of my objects that where in my arraylist where null so my comparator did not know what to do with them

    beginners mistakes .....sorry for the post i am pretty new

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: help with sorting...(comparator)

    Glad you solved your issue mdstrauss
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM