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:
Code :
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:
Code :
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....
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
Re: help with sorting...(comparator)
Glad you solved your issue mdstrauss :)