2

I have an URL where some data is stored, like

[
   {"id":1,"first_name":"add","last_name":"add"},
   {"id":2,"first_name":"sfdf","last_name":"aad"}
]

etc. I want to sort it by last name and display already sorted array in React functional component. How can I achieve this?

UPD. I have a problem with transforming data from url to an accessible js array, not with sort it

this is how I fetched data from URL

function App() {
  const [contacts, setContacts] = useState([]);

  const fetchContactsList = async() => {
    try {
      let response = await fetch(URL)
      let contacts = await response.json()
      setContacts(contacts)
    } catch (error) {
      console.log(error)
    }
  }
  useEffect(() => {
    fetchContactsList()
  }, [])


  return (
    <main>
      <Container>
        <Header />
        <Search />
        <ContactsList contacts={contacts} />
      </Container>
    </main>
  );
}
2

3 Answers 3

2

Edits:

  • Edited answer to better reflect OPs clarification in the question
  • Updated sort function to use localeCompare

I think what you're looking for is something like this:


function App() {
  const [contacts, setContacts] = useState([]);

  const fetchContactsList = async() => {
    try {
      let response = await fetch(URL)
      let contacts = await response.json()
      setContacts(contacts)
    } catch (error) {
      console.log(error)
    }
  }
  useEffect(() => {
    fetchContactsList()
  }, [])

  const sortedContacts = useMemo(() => {
    if (!contacts) return;

    return contacts.sort((a,b) => a.last_name.localeCompare(b.last_name));
  }, [contacts]);

  return (
    <main>
      <Container>
        <Header />
        <Search />
        <ContactsList contacts={sortedContacts} />
      </Container>
    </main>
  );
}

Sign up to request clarification or add additional context in comments.

Comments

1

U can just use .sort()

function sorting(){
    var array = [{last_name:'z'},{last_name:'s'}]

    var array2 = ['a','s','d']

    return array.sort(dynamicSort("last_name"));
}

function dynamicSort(property) {
    var sortOrder = 1;
    if(property[0] === "-") {
        sortOrder = -1;
        property = property.substr(1);
    }
    return function (a,b) {
        var result = (a[property] < b[property]) ? -1 : (a[property] > b[property]) ? 1 : 0;
        return result * sortOrder;
    }
}
 
console.log (sorting())

enjoy

Comments

1

This single line was an answer

contacts.sort( (a,b) => a.last_name.localeCompare(b.last_name))


  function App() {
  const [contacts, setContacts] = useState([]);

  const fetchContactsList = async() => {
    try {
      let response = await fetch(URL)
      let contacts = await response.json()
      contacts.sort( (a,b) => a.last_name.localeCompare(b.last_name))
      setContacts(contacts)
    } catch (error) {
      console.log(error)
    }
  }
  useEffect(() => {
    fetchContactsList()
  }, [])


  return (
    <main>
      <Container>
        <Header />
        <Search />
        <ContactsList contacts={contacts} />
      </Container>
    </main>
  );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.