How do I sort a list of string and numbers in numerical order?

How do I fix id’s sorting in a non-formal way like this:

 1
10
11
..
2
20
21
..

I want to print like:

1
2
3
..
10
11
..

Also, my MainData.getName contains strings and numbers up to list of ListArrays of 1000, see my app image link:

I tried this but it gives the same result

int[] range = IntStream.rangeClosed(1, 1000).toArray();
List<String> rangeList = new ArrayList<>();
for (int number : range) {
    rangeList.add(String.valueOf(number));
}

Here is my MainActivity

Api api = retrofit.create(Api.class);
    Call<List<MainData>> call = api.getData();
    call.enqueue(new Callback<List<MainData>>() {
        @Override
        public void onResponse (Call<List<MainData>> call, Response<List<MainData>> response) {
            if (!response.isSuccessful()) {
                Toast.makeText(MainActivity.this, response.code(), Toast.LENGTH_SHORT).show();
                return;
            }
            List<MainData> postList = response.body();
            // Filter out any items where "name" is blank or null.
            List<MainData> tempList = new ArrayList<>();
            for(MainData data :postList)
            {

                if(null!= data.getName() && !data.getName().isEmpty()) {

                     //sort by name
                    Collections.sort(tempList, (mainData, t1) -> mainData.getName().compareTo(t1.getName()));

                    int[] range = IntStream.rangeClosed(1, 1000).toArray();
                    List<String> rangeList = new ArrayList<>();
                    for (int number : range) {
                        rangeList.add(String.valueOf(number));
                    }

                    //sort by ListId
                    Collections.sort(tempList, (mainData, t1) -> mainData.getListId().compareTo(t1.getListId()) );

                    tempList.add(data);

                }
            }

            RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(tempList, MainActivity.this);
            recyclerView.setAdapter(recyclerViewAdapter);
        }

        @Override
        public void onFailure (Call<List<MainData>> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

And here is my Json data

public class MainData {

    public String listId, name, id;

    public String getListId () {
        return listId;
    }

    public  String getName () {
        return name;
    }

    public  String getId () {
        return id;
    }
}

Answer

Since your listId is String ,the list gets sorted lexographically. Instead you could first convert the String to an Integer and then sort it. Below is the illustration.

Collections.sort(tempList, (mainData, t1) -> Integer.parseInt(mainData.getListId())-Integer.parseInt(t1.getListId());