-2

I Have one Custom arraylist which contains multiple parameters along with time.

I want to sort that list according to time Parameter in asending and desending format

I searched alot Here are some links Sort objects in ArrayList by date?

Sort ArrayList with times in Java

and many more but none worked

Here is my code

function calling on Click

 private void callTimeSorting(ArrayList<ObjectClass>solutions){
    try{
        ArrayList<ObjectClass> tempList = new ArrayList<>();
        if (tempList.size() > 0){tempList.clear();}
        tempList.addAll(solutions);
        if (mTimeTag == 1) {
            if (!mAsendingTime) {
                Collections.sort(tempList, AsendingTimeComparator);
                mAsendingTime = true;
                mAdapter.notify();
            } else {
                Collections.sort(tempList, DesendingTimeComparator);
                mAsendingTime = false;
            }

        }
 }

OnClick method

 @Override
public void onClick(View view) {
    int id = view.getId();
    switch (id) {
       case R.id.tvTimings :
            mTimeTag =  1 ;
            callTimeSorting(mSolutions) ;
            mAdapter.notifyDataSetChanged();
            break;


 }}

AsendingComparator

  private static Comparator<ObjectClass> AsendingTimeComparator = new Comparator<ObjectClass>() {
    @Override
    public int compare(ObjectClass solution1, ObjectClass solution2) {
        String time1 = solution1.getDepartureDateTime();
        String time2 = solution2.getDepartureDateTime();
        return time1.compareTo(time2);//asending
    }
};

private static Comparator<ObjectClass> DesendingTimeComparator = new Comparator<ObjectClass>() {
    @Override
    public int compare(ObjectClass solution1, ObjectClass solution2) {
        String time1 = solution1.getDepartureDateTime();
        String time2 = solution2.getDepartureDateTime();
        return time2.compareTo(time1);//descending

    }



};

I dont know why my sorted list is not attaching to recyclerView??

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pranita
  • 803
  • 7
  • 16

3 Answers3

3

Use the original list used in Adapter, not the temp one

 private void callTimeSorting(ArrayList<ObjectClass>solutions){
        try{
            ArrayList<ObjectClass> tempList = new ArrayList<>();
            tempList.addAll(solutions);
            solutions.clear();
            if (mTimeTag == 1) {
                if (!mAsendingTime) {
                    Collections.sort(tempList, AsendingTimeComparator);
                    mAsendingTime = true;
                } else {
                    Collections.sort(tempList, DesendingTimeComparator);
                    mAsendingTime = false;
                }
              solutions.addAll(tempList);
              mAdapter.notifyDataSetChanged();
            }
     }
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
2

Your list is not getting sorted because you are sorting the tempList:

Collections.sort(tempList, AsendingTimeComparator);

And not the actual list your adapter is using i.e solutions.

You should not use templist and directly sort solutions.

private void callTimeSorting(ArrayList<ObjectClass>solutions){
    try{
        if (mTimeTag == 1) {
            if (!mAsendingTime) {
                Collections.sort(solutions, AsendingTimeComparator);
                mAsendingTime = true;
                mAdapter.notify();
            } else {
                Collections.sort(solutions, DesendingTimeComparator);
                mAsendingTime = false;
            }

        }
 }
Nargis
  • 4,687
  • 1
  • 28
  • 45
1

Use the reference of the mList ... through out your code dont initialize it again.. use clear() instead of new Arraylist<> and dont assign any other reference to the list like list = new_List;

The mAdapter would lose reference of the list... and wont work accordingly.. (notifyDataSetChanged() wont work.

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52