-1
  1. For instance show only automobile that are; “Mercedes” or ‘’Nissan?”
  2. Or by YearOfManufacture; only automobile manufactured in 2004 in my listView?

My Automobil class looks like this

public class AutoMobil {
     private String make;
     private String brandName;
     private int yearOfManufacture;
  }

My autoMobil populated list looks like this;

private void autoMobileList() {
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Corolla’’, ‘’2011’’));
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Corolla’’, ‘’2002’’));
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Mark2’’, ‘’1998’’));
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Camry’’, ‘’2004’’));
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Corona’’, ‘’2011’’));
    MyAutoMobilList.add(new AutoMobil(“Nissan’’, ‘’March’’, ‘’2013’’));
    MyAutoMobilList.add(new AutoMobil(“Nissan’’, ‘’Micra’’, ‘’2001’’));
    MyAutoMobilList.add(new AutoMobil(“Nissan’’, ‘’Laurel’’, ‘’2014’’));
    MyAutoMobilList.add(new AutoMobil(“Mercedes’’, ‘’SL500’’, ‘’2004’’));
    MyAutoMobilList.add(new AutoMobil(“Mercedes’’, ‘’190E’’, ‘’1992’’));
    MyAutoMobilList.add(new AutoMobil(“Mercedes’’, ‘’C200’’, ‘’1993’’));
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Hiace’’, ‘’1998’’));
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Hiace’’, ‘’2003’’));
    MyAutoMobilList.add(new AutoMobil(“Toyota’’, ‘’Vitz’’, ‘’2004’’));

}
udondan
  • 57,263
  • 20
  • 190
  • 175
Kim
  • 1
  • 3
  • http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – falsetto Apr 10 '15 at 13:04
  • possible duplicate of [Android-java- How to sort a list of objects by a certain value within the object](http://stackoverflow.com/questions/9109890/android-java-how-to-sort-a-list-of-objects-by-a-certain-value-within-the-object) – Haresh Chhelana Apr 10 '15 at 13:11

2 Answers2

1

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

Community
  • 1
  • 1
Vikalp
  • 2,051
  • 4
  • 18
  • 24
0

Sort by Brand Name (String):

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

Sort by Year Of Manufacture (int):

Collections.sort(conversations, new Comparator<AutoMobil>(){
                public int compare(AutoMobil s1, AutoMobil s2) {
        if (s1.getYearOfManufacture() < s2.getYearOfManufacture())
            return 1;
        if (s1.getYearOfManufacture() > s2.getYearOfManufacture())
            return -1;
        else
            return 0;
    }
});

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));
Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63
  • Thanks a bunch it works perfect in java. But I still have problems or errors when placing the collections.sort instances in my android activity. Should codes be applied to my adapter? if so, How should I do it? – Kim Apr 11 '15 at 22:06
  • @Kim make sure you have this import java.util.Collections; – Volodymyr Kulyk Apr 14 '15 at 09:34