I have a ArrayList<CustomObject> myFinalList = new ArrayList<CustomObject>();
The list has a ojbect property "NumberOfLikes" for each entry in the list. "NumberOfLikes" are String values.
I have derived these values into an array like so:-
ArrayList<String> numberOfLikes = new ArrayList<String>();
for(int j =0; j<myFinalList.size(); j++)
{
String likes = myFinalList.get(j).getNewsLikes();
numberOfLikes.add(likes);
}
numberofLikesArray= numberOfLikes.toArray(new String[numberOfLikes.size()]);
int[] numberofLikesArrayInt = new int[numberofLikesArray.length];
for (int n = 0; n < numberofLikesArray.length; n++)
{
numberofLikesArrayInt[n] = Integer.parseInt(numberofLikesArray[n]);
}
Arrays.sort(numberofLikesArrayInt);
ArrayList<Integer> intAarrylistlikes = new ArrayList<Integer>();
for (int i = 0; i < numberofLikesArrayInt.length; i++)
{
intAarrylistlikes.add(numberofLikesArrayInt[i]);
}
Collections.reverse(intAarrylistlikes);
numberofLikesArrayInt = convertIntegers(intAarrylistlikes);
numberofLikesArray = Arrays.toString(numberofLikesArrayInt).split("[\\[\\]]")[1].split(", ");
for (int i = 0; i < numberofLikesArray.length; i++)
{
companyNewsList = Lists.newArrayList(Collections2.filter(myFinalList, new ArticleFilter2(numberofLikesArray[i])));
}
ArticleFilter2.java
public class ArticleFilter2 implements Predicate<News>
{
private final Pattern pattern;
public ArticleFilter2(final String regex)
{
pattern = Pattern.compile(regex);
}
@Override
public boolean apply(final CustomObject input)
{
return pattern.matcher(input.getLikes()).find();
}
}
Now this array consists of the "NumberOfLikes" in descending order. Now I want to create another ArrayList which sorts the "MyFinalList" according to these values ie:- item having the maximum likes should appear first.
How can I do it?
This asks for a Comparator
with your Custom Class. http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html
Make a new class that implements Comparator and implement this method:
public class YourComparator<CustomClass> implements Comparator{
@Override
public int compare ( CustomClass obj1, CustomClass obj2 ) {
return (int) (obj1.getYourProperty() - obj2.getYourProperty());
}
}
Use it:
Collections.sort(MyFinalList, new YourComparator());
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.