1

I am new to stackoverflow.

Here is my question

This is about Android. I am implementing recycler view which have heterogeneous layouts.I am passing a static data to it. So if its Layout A it takes object A data. If its Layout B it takes Object B data and so on.

Example :

List.add(new A("May 27",2,3000$));

List.add(new B("April 21",2,"Place1",2000$));

List.add(new c("March 20","from place","toplace",5000$));

My Question is how i can sort the Recyclerview based on the date that we have passed for each object. let say "March 20" comes first so it has to appear at top in the recyclerview and "May 27" should display last in the Recyclerview.

I can use Collections.sort() by passing the list and the implementation of comparator interface as second argument. by taking away the date field and inserting it into parent class D and making class A,B,C inherit Class D. but its not working in my case.

Class D {

    private String date;

    // getter and setter method

}

Class A extends D{}

Class B extends D{}

Class C extends D{}

IN RecyclerViewAdapter

//onCreateViewHolder()


//OnBindViewHolder()

//getiewType()


//Creating list of data

private ArrayList<Object> getData(){

        ArrayList<Object> list = new ArrayList<>();

        for(int i=0;i<=20;i++) {

            list.add(new A("MAY 23", 1, 3000$));

            list.add(new B("April27", 1, 2, 3400$));

            list.add(new C("New york" "March 28", 1000$, 2, 2));

        }


    Collections.sort(list, new Comparator<D>() {

        @Override
        public int compare(D l1, D l2) {

            SimpleDateFormat sdf = new SimpleDateFormat("MMM dd", Locale.ENGLISH);

            Date d1=null;
            Date d2= null;

            try {
                d1=sdf.parse(String.valueOf(l1.getDate()));

                 d2= sdf.parse(String.valueOf(l2.getDate()));

             } catch (ParseException e) {

                e.printStackTrace();
            }

            if(d1 != null && d1.after(d2)){

                return -1;

            }else{

                return 1;}
            }

        return 0;

##error "unexpected token" /// or if do something here it shows entire block ##as error :

Wrong in 2nd argument

Found : java.util.comparator(D)

required : java.util.comparator(Object)

    });

    return list;

}

can some one please help me on this question. Thanks in advance

6
  • 2
    If you can make ABC extend D then you can just have ABC implement a common interface that provides a date, then you just sort on date Commented Apr 16, 2017 at 3:18
  • Thanks for the quick response. can you please help me with the code that will help me more Commented Apr 16, 2017 at 3:30
  • @Mohan use this link ideone.com/Lz6VoC i added for only for one case do it for remaining classes. Its too much boiler plate not an ideal solution but hope it helps in this regard. Commented Apr 16, 2017 at 4:34
  • is there any simple way to do its too much code to go. Thanks for your help @smashcode Commented Apr 16, 2017 at 5:01
  • wrap code existing after a instanceof b to a method and pass class objects as parameter then there will be no need to write same code 3 times Commented Apr 16, 2017 at 5:04

2 Answers 2

1

You should get your desired output if you're passing correct comparator. It should look like this:

Collections.sort(list, new Comparator<ListType>() {
    @Override
    public int compare(ListType l1, ListType l2) {
        return l1.getDate().compareTo(l2.getDate());
    }
}); 

If it doesn't work, then check whether you're setting date correctly. I see that here you're passing date as a String. Are you parsing it correctly while setting it as a date?


EDIT

You're not parsing your date correctly. To parse these dates

May 27
April 21
March 20

You should use:

...
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd", Locale.ENGLISH);
...
Sign up to request clarification or add additional context in comments.

3 Comments

Yes i even did that by using "Simpledateformat" class and using "parse" method to return date object and wrote the sorting code but when i pass the comparator to collections as (Collection.sort(list,new ComparatorImp) it is not accepting
@Mohan please edit your question and post come code.
Yes i changed the parsing format but the error is still remain same
0

Finally i got the answer here is my solution

Solution pass Object class in Comparator and type cast it to ClassD :)

private ArrayList getData(){

    ArrayList<Object> list = new ArrayList<>();



        list.add(new A("MAY 23", 1, 3000$));

        list.add(new B("April27", 1, 2, 3400$));

        list.add(new C("New york" "March 28", 1000$, 2, 2));




Collections.sort(list, new Comparator<Object>() {

    @Override
    public int compare(Object l1, Object l2) {

        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd", Locale.ENGLISH);

        Date d1=null;
        Date d2= null;

        try {
            d1=sdf.parse(String.valueOf((D)l1.getDate()));

             d2= sdf.parse(String.valueOf((D)l2.getDate()));

         } catch (ParseException e) {

            e.printStackTrace();
        }

        if(d1 != null && d1.after(d2)){

            return 1;

        }else if(d!=null && d1.before(d2)){

            return -1;
        }else return 0;

} return list;

1 Comment

Looks to me like all the parsing of string dates is not needed. If getDate returns a Date object.

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.