Sorting of ArrayList

后端 未结 5 2261
渐次进展
渐次进展 2020-12-10 22:24

I have one array list

ArrayList itemListWithRank = ItemListDAO.getItemList();

and in arraylist itemListWithRank there are lot

相关标签:
5条回答
  • 2020-12-10 22:54

    First of all, every objects in the ArrayList must have some common parent in their hierarchy or implements an interface which define some way to get the rank. For example, all of them must implement this interface :

    interface Rankable {
        public int getRank();
    }
    

    The you can create a custom Comparator :

    Comparator<Rankable> myComparator = new Comparator<Rankable>() {
        public int compare(Rankable o1, Rankable o2) {
            return o1.getRank() - o2.getRank();
        }
        public equals(Object obj) {
            return obj == this;
        }
    }
    

    And finally sort your ArrayList :

    Collections.sort(itemListWithRank, myComparator);
    

    You can also implements Comparable in all your objects in the ArrayList and then the legacy sort method, but this will be less flexible if you're planning on doing other kind of comparison on them.

    0 讨论(0)
  • 2020-12-10 23:02
    Collections.sort(itemListWithRank ,new Comparator<Person>() {
    
        public int compare(Person o1, Person o2) {
            return Integer.valueOf(o1.id).compareTo(Integer.valueOf(o2.id));
        }
    });
    
    0 讨论(0)
  • 2020-12-10 23:03

    Consider using lambdaj, which allows this construct

    List<Person> sorted = sort(persons, on(Person.class).getAge());
    
    0 讨论(0)
  • 2020-12-10 23:04

    Use Collections.sort(List<T> list, Comparator<? super T> c) and pass a custom comparator for your DAO objects.

    It's by far easier if all of your list items share a common supertype that provides a method to get the item rank. Assuming you have such an interface, let's call it RankProvider, the comparator could look like:

    public class Comparator<RankProvider> {
      @Override
      public int compare(RankProvider o1, RankProvider o2) {
        return o1.getItemRank().compareTo(o2.getItemRank());
      }
    }
    

    Pass an instance of this comparator or define an anonymous class.

    Note - the example give above assumes, that the item rank is either a java primitive (like int) or a String or, in other words, is a Comparable (directly or after inboxing)


    If you don't have a common superclass or interface, then comparing is less trivial. You'll either have to know all possible types and handle them each by each or you know that all types have the same method (name) and you can reflect the rank. One example for a comparator that compares known but random types:

    public class Comparator {  // no generics this time
      @Override
      public int compare(Object o1, Object o2) {
         Object[] comparables = new Object{o1, o2};
         int[] ranks = new int[2];
    
         for (int i = 0; i < 2; i++) {
           if (comparables[i] instanceof MyType1) {
             ranks[i] = ((MyType1) comparables[i]).getRank(); // rank getter for MyType1 type
             continue;
           }
    
           if (comparables[i] instanceof MyType2) {
             ranks[i] = ((MyType2) comparables[i]).getRank(); // rank getter for MyType2 type
             continue;
           }
    
           // ...
         }
         return ranks[0] - ranks[1];  // ascending order
      }
    }
    

    This could be done if you have no chance to refactor your DAOs to implement a shared interface.

    0 讨论(0)
  • 2020-12-10 23:06

    Make them all object of a type. either design a common Base class or an Interface

    and then

    use Comparator to sort them out

    For example.

    public class SortableFields{
      protected long rank;
      //accessors methods
    }
    

    assumed that all the objects in arraylist are SortableFields now

    Now

    Collections.sort(list,new Comparator(){
    public int compareTo(Object ob1,Object ob){
      return ((SortableFild)ob1.getRank())-((SortableFild)ob2.getRank())
    }
    });
    

    Or use reflection hack , not preferable

    Collections.sort(list,new Comparator(){
    public int compareTo(Object ob1,Object ob){
         UtilClass.getRank(ob1)-UtilClass.getRank(ob);      
    }
    });
    

    In your UtilClass

    public int getRank(Object ob){
    
          Class cl=ob1.getClass();
          Method mthd=cl.getMethod("getRank");
          Integer output=(Integer)mthd1.invoke(ob);
          return output;
    
    }
    
    0 讨论(0)
提交回复
热议问题