0

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?

3
  • Is it possible to add a property (getter and setter) for the numberOfLikes to the CumstomObject class? Commented May 5, 2014 at 8:46
  • @Bob yes. I already have set the getters and setters Commented May 5, 2014 at 8:53
  • Then you can use AndroidGuys awnser to sort the list. Commented May 5, 2014 at 8:56

1 Answer 1

2

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());

Sign up to request clarification or add additional context in comments.

2 Comments

The property i want to sort the List is a String
It doesn't matter what type this list is. In the compare method you can implement your own compare logic on those string values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.