ArrayList Sort

I have a list of arrays

ArrayList itemListWithRank = ItemListDAO.getItemList(); 

and in arraylist itemListWithRank there are many types of object values, they are all different. And one value from them is the rank of the element, which is also given with this list of arrays.

Now I want to sort this list of arrays based on divergent rank order. The ranked value is already set in this list of arrays.

How can I sort an arraylist that has many types of values ​​....?

Thanks to everyone ....

+2
java sorting arraylist
Mar 15 '11 at 8:27
source share
6 answers

Make everything an object of type. either create a common base class or Interface

and then

use Comparator to sort them

For example.

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

assumes all objects in arraylist are now SortableFields

Now

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



Or use reflex hacking rather than 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; } 
+6
Mar 15 2018-11-11T00:
source share

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

This is much simpler if all the items in the list have a common supertype, which provides a method for getting the ranking of an item. Assuming you have such an interface, call it RankProvider , the comparator might look like this:

 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 above example assumes that the ranking of an element is either a java primitive (e.g. int ), or a string, or, in other words, is Comparable (directly or after the incoming)




Unless you have a common superclass or interface, the comparison is not trivial. You either need to know all the possible types, or process them for each of them, 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 can be done if you do not have the ability to reorganize your DAOs to implement a common interface.

+4
Mar 15 '11 at 8:32
source share
 Collections.sort(itemListWithRank ,new Comparator<Person>() { public int compare(Person o1, Person o2) { return Integer.valueOf(o1.id).compareTo(Integer.valueOf(o2.id)); } }); 
+2
Mar 15 2018-11-11T00:
source share

Consider using lambdaj , which allows you to use this construct

 List<Person> sorted = sort(persons, on(Person.class).getAge()); 
+1
Mar 15 2018-11-11T00: 00Z
source share

Use the comparable method to override the compareto interface and return + ve if the passed value is larger than the current object and vice versa - see
http://lkamal.blogspot.com/2008/07/java-sorting-comparator-vs-comparable.html

0
Mar 15 2018-11-11T00:
source share

First of all, all objects in the ArrayList must have some common parent in their hierarchy or implement an interface that determines how to get the rank. For example, they should all implement this interface:

 interface Rankable { public int getRank(); } 

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 implement Comparable in all of your objects in an ArrayList and then an inherited sorting method, but it will be less flexible if you plan on doing another comparison with them.

0
Mar 15 2018-11-11T00:
source share



All Articles