1

I am developing an app in which i want to sort the price coming from API to 1. Low to high 2. High to low

Is any method in recylerView to Arrange items in Accending and Decending order.

This is My Adapter OnBindViewHolder method:

  @Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    typeface = Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-SemiBold.ttf");
    String email = doctorList.get(position).getDoctor().getDoctor_email();
    doctor_id = doctorList.get(position).getDoctor().getDoctor_id();

     holder.dr_name.setText(doctorList.get(position).getDoctor().getDoctor_name());
     holder.dr_exp.setText(doctorList.get(position).getDoctor().getDoctor_exp()+" exp.");
     holder.dr_fee.setText("Rs. "+doctorList.get(position).getDoctor().getDoctor_fee());
     Log.e("FEE",""+doctorList.get(position).getDoctor().getDoctor_fee());

    // holder.dr_imageDL.setImageURI(Uri.parse(doctorList.get(position).getDoctor().getDoctor_img()));
    Picasso.with(context)
            .load(doctorList.get(position).getDoctor().getDoctor_img())
            .memoryPolicy(MemoryPolicy.NO_CACHE)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .resize(200, 200)
            .placeholder(R.drawable.doctor_img)
            .into(holder.dr_imageDL);

    holder.bookBtn.setTypeface(typeface);
    holder.bookBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(view.getContext(), BookAppointment.class);
            intent.putExtra("DOCTOR_ID",doctor_id);
            view.getContext().startActivity(intent);
             Log.e("CLICK","CLICK"+doctor_id);
        }
    });

}
Abhishek Kumar
  • 1,255
  • 13
  • 21

3 Answers3

4

You should sort you doctorList list, and then call adapter.notifyDataSetChanged(); to re-draw your RecylerView view.

AlexTa
  • 5,133
  • 3
  • 29
  • 46
3

You can see this code for help

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestSort {

public static void main(String args[]){

    ToSort toSort1 = new ToSort(new Float(3), "3");
    ToSort toSort2 = new ToSort(new Float(6), "6");
    ToSort toSort3 = new ToSort(new Float(9), "9");
    ToSort toSort4 = new ToSort(new Float(1), "1");
    ToSort toSort5 = new ToSort(new Float(5), "5");
    ToSort toSort6 = new ToSort(new Float(0), "0");
    ToSort toSort7 = new ToSort(new Float(3), "3");
    ToSort toSort8 = new ToSort(new Float(-3), "-3");

    List<ToSort> sortList = new ArrayList<ToSort>();
    sortList.add(toSort1);
    sortList.add(toSort2);
    sortList.add(toSort3);
    sortList.add(toSort4);
    sortList.add(toSort5);
    sortList.add(toSort6);
    sortList.add(toSort7);
    sortList.add(toSort8);

    Collections.sort(sortList);

    for(ToSort toSort : sortList){
        System.out.println(toSort.toString());
    }
}

}

public class ToSort implements Comparable<ToSort> {

private Float val;
private String id;

public ToSort(Float val, String id){
    this.val = val;
    this.id = id;
}

@Override
public int compareTo(ToSort f) {

    if (val.floatValue() > f.val.floatValue()) {
        return 1;
    }
    else if (val.floatValue() <  f.val.floatValue()) {
        return -1;
    }
    else {
        return 0;
    }

}

@Override
public String toString(){
    return this.id;
}
}
Anmol317
  • 1,356
  • 1
  • 14
  • 31
3

you can use comparator to sort objects in list based on specific value, in your case you can have two comparators like ascending and descending.. Here you have a work around for sorting list based on age, u can implement same for price in your object http://www.javatpoint.com/Comparator-interface-in-collection-framework Also u can implement this by single comparator pls check this link also: https://stackoverflow.com/a/1947527/6350239

Community
  • 1
  • 1
Moulesh
  • 2,762
  • 1
  • 22
  • 32