Search code examples
androidsortingarraylistandroid-arrayadaptercomparator

Sorting ArrayList with objects using Comparator and Custom ArrayAdapter


How do I sort properly ArrayList of objects using Comparator and then pass this sorted array to custom array adapter?

I have tried like that:

Content class of ArrayList:

public class CoursesData {
private String url;
private String name;
private String lessonsCount;
private String isFree;
private String price;
private String language;
private String difficulty;

private AuthorData author;

.....getters-setters
}

Comparator:

class CoursesPriceComparator implements Comparator <CoursesData> {
public int compare (CoursesData left, CoursesData right){
    return left.getPrice().compareTo(right.getPrice());
}
}

Setting the adapter in OnCreate:

        mAdapter = new CoursesItemAdapter(getActivity(),
            R.layout.catalog_list_item,
            Collections.sort(parserJson.getCourses(), new CoursesPriceComparator()));

..and getting error here: enter image description here

...since I'm using custom ArrayAdapter which is like:

class CoursesItemAdapter extends ArrayAdapter<CoursesData> {
private Activity myContext;
private ArrayList<CoursesData> datas;

public CoursesItemAdapter(Context context, int textViewResourceId, ArrayList<CoursesData> objects){
    super(context, textViewResourceId, objects);

    myContext = (Activity) context;
    datas = objects;
}

public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder viewHolder;

    if(convertView == null) {
        LayoutInflater inflater = myContext.getLayoutInflater();
        convertView = inflater.inflate(R.layout.catalog_list_item, null);

        viewHolder = new ViewHolder();
        viewHolder.postNameView = (TextView) convertView.findViewById(R.id.courses_name);
        viewHolder.postDifficultyView = (TextView) convertView.findViewById(R.id.difficulty);
        viewHolder.postAuthorView = (TextView) convertView.findViewById(R.id.author);

        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }


    viewHolder.postNameView.setText(datas.get(position).getName());
    viewHolder.postAuthorView.setText(datas.get(position).getAuthor().getName());
    viewHolder.postDifficultyView.setText(datas.get(position).getDifficulty());

    return convertView;
}
    static class ViewHolder{
    TextView postNameView;
    TextView postAuthorView;
    TextView postDifficultyView;
}

How do I need to modify this adapter to make it working with Comparable?

Thanks for any help.


Solution

  • Your assumption that Collections.sort( returns the sorted collection is wrong.

    mAdapter = new CoursesItemAdapter(getActivity(),
                R.layout.catalog_list_item,
                Collections.sort(parserJson.getCourses(), new CoursesPriceComparator()));
    

    it should be

    ArrayList< CoursesData > data = parserJson.getCourses();
    Collections.sort(parserJson.getCourses(), new CoursesPriceComparator())
    mAdapter = new CoursesItemAdapter(getActivity(),
                R.layout.catalog_list_item,
                data);
    

    Collection.sort return type is void. Check here the documentation