Navigate back to the homepage

how to sort array of string in react js?

Infinitbility
React
July 10th, 2022 · 1 min read
how to sort array of string in react js?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To sort array of string in react js, use the sort() method with localeCompare() method it will sort array and return new array based on your condition.

The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted. The default sort order is ascending.

The localeCompare() method returns a number indicating whether a reference string comes before, or after, or is the same as the given string in sort order.

For the sort array of strings alphabetically, let’s take an example of strings array.

1var items = ['infinitbility', 'aguidehub', 'sortoutcode'];
2
3
4items.sort((a, b) =>
5 a.localeCompare(b)//using String.prototype.localCompare()
6);
7
8// 👇️ Output
9//  ['aguidehub', 'infinitbility', 'sortoutcode']

Today, I’m going to show you How do I sort array of string in react js, as above mentioned here, I’m going to use the sort() method with localeCompare() method.

Let’s start today’s tutorial how do you sort array of string in react js?

In this example, we will do

  1. create a sample array of strings state
  2. used sort function to sort state
  3. update the state with sorted array
  4. print the array on screen

Let’s write code…

1import React, { useState, useEffect } from "react";
2export default function App() {
3 const [arr, setArr] = useState(['infinitbility', 'aguidehub', 'sortoutcode']);
4
5 useEffect(() => {
6 // sort the array
7 var items = [...arr];
8 items.sort((a, b) =>
9 a.localeCompare(b)
10 );
11 setArr(items);
12 }, []);
13
14 return (
15 <div className="App">
16 <p>{JSON.stringify(arr)}</p>
17 </div>
18 );
19}

As mentioned above, we are taken the example of a array of string state, used the sorting methods to sort array of string and print the output.

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to center text in react native?

how to center text in react native?

An example of center text in react native with step by step guide.

July 9th, 2022 · 1 min read
how to check empty object in node js?

how to check empty object in node js?

An example of check empty object in node js with step by step guide.

July 9th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]