简体   繁体   中英

How to sort an array list according to different criteria?

Hi everyone (if my english is bad sorry), I have to code an Android Application at school. The project concerns a music player, i got an arraylist of musics, and i want to be able to sort them by differents criteria (Artist, Title and Album). I already done some code but it only work for the titles. I let you my code below, i want to modify typeTri according to the button pressed, i got a clicklistener on each button (Artist,Title and Album). Thank you for your help

@Override
    public int compareTo(Musique a){
        String typeTri = "Artistes";
        if (typeTri.equals("Artistes")){

            if (a.getArtiste().compareTo(this.getArtiste())>0)
                return -1;
            if (a.getArtiste().compareTo(this.getArtiste())<0)
                return 1;
            if (a.getArtiste().equals(this.getArtiste()))
                return 0;
        }

        if (typeTri.equals("Albums")){

            if (a.getAlbum().compareTo(this.getAlbum())>0)
                return -1;
            if (a.getAlbum().compareTo(this.getAlbum())<0)
                return 1;
            if (a.getAlbum().equals(this.getAlbum()))
                return 0;
        }

        if (typeTri.equals("Titres")){

            if (a.getTitre().compareTo(this.getTitre())>0)
                return -1;
            if (a.getTitre().compareTo(this.getTitre())<0)
                return 1;
            if (a.getTitre().equals(this.getTitre()))
                return 0;
        }
    return 0;
    }

It looks in your code like you are making Musique implement Comparable<Musique> . Referring to the Javadoc of that class:

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering...

If you want to sort things by different criteria depending upon the context, then you are not sorting them by a natural order. As such, it is not appropriate to use Comparable in this case, but rather to define external Comparator s.

It would be easiest to have several instances of Comparator<Musique> , one for each of your current typeTri values:

enum MusiqueComparator implements Comparator<Musique> {
  ARTISTE {
    @Override public int compare(Musique a, Musique b) {
      return b.getArtiste().compareTo(a.getArtiste());
    }
  },
  ALBUM {
    @Override public int compare(Musique a, Musique b) {
      return b.getAlbum().compareTo(a.getAlbum());
    }
  },
  TITRE {
    @Override public int compare(Musique a, Musique b) {
      return b.getTitre().compareTo(a.getTitre());
    }
  }
}

Now, when you click the button to sort by, eg Artist, pass MusiqueComparator.ARTISTE to Collections.sort to sort your list by that criterion.

In your onClickListener try this

if(musiqueList!=null) {
            Collections.sort(musiqueList, new Comparator<Musique>() {
                @Override
                public int compare(Musique item1, Musique item2) {
                    if(typeTri.equals("Titres"){
                        //compare your items by Titres
                     } else if(typeTri.equals("Albums")
                        //compare your items by Albums
                     else //compare your items by Artistes
                }
            });
        }

You can easily sort any Collection (List<> is a Collection) using the following function:

Collections.sort(arrayListToSort, new MyComparator());

You will need to provide your own logic for comparing llike this:

class MyComparator implements Comparator<YourType>

{

@Override
 public int compare(YourType lhs, YourType rhs)
 {
    //your logic for comparing these two, you need to return int, 0 if they're equal, negative value if lhs is greater, positive if it's smaller
 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM