0

In my application i want add some data from server into adapter of RecyclerView.
I write below codes and i can get data from server and show into RecyclerView.
But my json in not sorted with Alphabet from server.
I want before add this data into ArrayList, first sort this with Alphabet and add into ArrayList.
My json :

{
    "validationCheckResult": {
        "isValid": true,
        "message": "Success"
    },
    "internalCarBoard": [{
        "id": 0,
        "name": “S500”,
        "companyName": “Benz”,
    }, {
        "id": 0,
        "name": " E250”,
        "companyName": “Benz”,
    }, {
        "id": 0,
        "name": “TT”,
        "companyName": “Audi”,
    }, {
        "id": 0,
        "name": " Talisman”,
        "companyName": “Renult”,
    }, {
        "id": 0,
        "name": " Accord”,
        "companyName": “Honda”,
    }, {
        "id": 0,
        "name": “A8”,
        "companyName": “Audi”,
    }]
}

I want sort above json from companyName with Alphabet before add to ArrayList.
But i want sort this into Android not server.

My Java codes:

public class SortListActivity extends AppCompatActivity {

    private RecyclerView sortList_recycler;
    private SortAdapter sortAdapter;
    private List<InternalCarBoard> model = new ArrayList<>();
    private API api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sort_list);
        sortList_recycler = findViewById(R.id.sortList_recycler);
        api = ApiClient.getClient().create(API.class);
        sortAdapter = new SortAdapter(model);
        sortList_recycler.setHasFixedSize(true);
        sortList_recycler.setLayoutManager(new LinearLayoutManager(this));
        sortList_recycler.setAdapter(sortAdapter);

        Call<VersionUpdateResponse> call = api.getCar();
        call.enqueue(new Callback<VersionUpdateResponse>() {
            @Override
            public void onResponse(Call<VersionUpdateResponse> call, Response<VersionUpdateResponse> response) {
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        if (response.body().getInternalCarBoard().size() > 0) {
                            model.clear();
                            model.addAll(response.body().getInternalCarBoard());
                            sortAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<VersionUpdateResponse> call, Throwable t) {

            }
        });
    }
}

How can i sort json with Alphabet from companyName before add into ArrayList?

Hock
  • 147
  • 2
  • 2
  • 14

3 Answers3

5

Use Collections.sort() like this

Collections.sort(model, new Comparator<InternalCarBoard>(){
            public int compare(InternalCarBoard obj1, InternalCarBoard obj2) {
                // ## Ascending order
                return obj1.getCompanyName().compareToIgnoreCase(obj2.getCompanyName); // To compare string values
                // return Integer.valueOf(obj1.getId()).compareTo(obj2.getId()); // To compare integer values

                // ## Descending order
                // return obj2.getCompanyName().compareToIgnoreCase(obj1.getCompanyName()); // To compare string values
                // return Integer.valueOf(obj2.getId()).compareTo(obj1.getId()); // To compare integer values
            }
        });

check this for details.

Man
  • 2,720
  • 2
  • 13
  • 21
  • But i want another question, can i sort first with companyName , then sort name. then add into recyclerView – Hock Jul 16 '18 at 09:46
  • you can check [this](https://stackoverflow.com/questions/4805606/how-to-sort-by-two-fields-in-java) link – Man Jul 16 '18 at 10:42
0

Please try following java 8 sorting API it will sort by ascending order

before adding to your new list

yourlist.sort((object1,object2)->object1.getCompanyName().compareTo(object2.getCompanyName())); // sort

yourlist.forEach(action->System.out.println(action.getCompanyName())); //iterate
Ganesh Gudghe
  • 1,327
  • 1
  • 17
  • 41
0

Sort your data before adding it to Adapter. You can try Collections.sort()

You can check this: How to sort RecyclerView item in android

Collections.sort(response.body().getInternalCarBoard(), new Comparator<InternalCarBoard>() {
        @Override
        public int compare(InternalCarBoard x, InternalCarBoard y) {
            // -1 - less than, 1 - greater than, 0 - equal, all inversed for descending
            return x.companyName > y.companyName;
        }
    });
Liar
  • 1,235
  • 1
  • 9
  • 19