简体   繁体   English

基于on对象属性对ArrayList进行排序

[英]Sort ArrayList based on a on object property

I have a ArrayList<CustomObject> myFinalList = new ArrayList<CustomObject>(); 我有一个ArrayList<CustomObject> myFinalList = new ArrayList<CustomObject>(); The list has a ojbect property "NumberOfLikes" for each entry in the list. 列表中的每个条目都有一个对象属性“ NumberOfLikes”。 "NumberOfLikes" are String values. “ NumberOfLikes”是字符串值。

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 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. 现在,此数组由降序排列的“ NumberOfLikes”组成。 Now I want to create another ArrayList which sorts the "MyFinalList" according to these values ie:- item having the maximum likes should appear first. 现在,我想创建另一个ArrayList,它根据这些值对“ MyFinalList”进行排序,即:-具有最大赞次数的项目应首先出现。

How can I do it? 我该怎么做?

This asks for a Comparator with your Custom Class. 这要求您的自定义类的Comparator http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Make a new class that implements Comparator and implement this method: 制作一个实现Comparator的新类并实现此方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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