Skip to content Skip to sidebar Skip to footer

Listview: How Do Sort My List Or Arraylist By A Specific Element?

For instance show only automobile that are; “Mercedes” or ‘’Nissan?” Or by YearOfManufacture; only automobile manufactured in 2004 in my listView? My Automobil class l

Solution 1:

Refer to this link. I have answered it before. Your requirement is similar to this one.

Solution 2:

Sort by Brand Name (String):

Collections.sort(list, new Comparator<AutoMobil>() {
        publicintcompare(AutoMobil v1, AutoMobil v2) {
        return v1.getBrandName().compareTo(v2.getBrandName());
    }
});

Sort by Year Of Manufacture (int):

Collections.sort(conversations, new Comparator<AutoMobil>(){
                publicintcompare(AutoMobil s1, AutoMobil s2) {
        if (s1.getYearOfManufacture() < s2.getYearOfManufacture())
            return1;
        if (s1.getYearOfManufacture() > s2.getYearOfManufacture())
            return-1;
        elsereturn0;
    }
});

Show only specific items:

ArrayList<AutoMobil> list_to_show = new ArrayList<AutoMobil>();
for (int i = 0; i < list.size(); i++)
     if (list.getBrandName().equals("Nissan"))
        list_to_show.add(list.get(i));

Post a Comment for "Listview: How Do Sort My List Or Arraylist By A Specific Element?"