Sort ArrayList by id react-native

Hi Guys, In this article, we going to learn about How to sort ArrayList by id in react-native, while developing react-native application developer’s need to save API response in state and use these data for display, this data mainly list and developers add his logic, algorithms on this list, sometimes we have to split our array list and after when needed we also merge both lists and make one list hence sometimes id order getting disturbed. so after doing lots of work on ArrayList we have to sort that list by id so that our data is being displayed properly. so let’s start

1. Create a new project

In my case, I have already created a project

2. Import the following components

3. Create ArrayList

I have created ArrayList with random id’s and define it in the constructor as follows, you can use list directly from API response

Now we have to sort the above list on the button Press

4. Function to sort ArrayList

As you saw in the above code the function returning sorted Arraylist by asc order after that we have to update previousState ArrayList using this.setState({previousState =>{demoList: previousState.demoList}});

Let’s see full source code so that you will get more clarity

5. Full source code(Home.js)

6. Output

Sort ArrayList by id react-native
Sort ArrayList by id react-native

Hope you will like this post, thank you 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *