title: Sorting Phone Contacts With React Native
body: <p>My react-native application sends something similar to messages to contacts in the users phonebook that have my application. So when users register they only register to use my app, they only register their phone number. When it comes to sending a message to a contact. I access their phonebook and compare it with numbers I have in my Rails backend. If they match a number in the users phonebook. Then I pull the name from their phone phonebook and display it to them along with the number to send a message to that contact. </p>
  
  <p>The issue is they are sorting randomly. I want them to sort alphabetically. The number comes from the BE (Rails) and the name comes from the FE (React-Native) app. I know I need to put them both into an array of some sort on the FE and then sort them. Unfortunatly my Javascript and React-Native skills are still somewhat weak. I know in theory what I need to do, I just can't seem to get it to work.</p>
  
  <p>I have of course left out quite a bit of the code and left in just the important parts. The first part shows the three functions "parseContacts, fetchContacts, getContactName.</p>
  
  <p>"fetchContacts" gets the phone number from the BE. "getContactName" gets the name from the phones phonebooks. "parseContacts" does what it says on the tin. </p>
  
  <pre><code>async parseContacts(contacts) {
      let numbers = [];
      contacts.forEach(element =&gt; {
        element.contactNumber = element.phoneNumbers[0] ? element.phoneNumbers[0].number.replace(/[^a-zA-Z0-9]/g, '') : '';
        element.phoneNumbers[0] ? numbers.push(element.phoneNumbers[0].number.replace(/[^a-zA-Z0-9]/g, '')) : null;
      });
      this.setState({ phoneContacts: contacts })
      return numbers;
    }
  
    async fetchContacts(contacts) {
      let phoneNumbers = await this.parseContacts(contacts);
      if (phoneNumbers.length) {
        this.setState({ isLoadingNumbers: true })
        let data = {
          "numbers": phoneNumbers,
        }
        try {
          let res = await postFunctionWithAuthToken(`${baseUrl}/contact/check`,
            JSON.stringify(data), this.state.authToken);
          if (res.response) {
            console.log('contacssssst: ', res.contact_list);
            this.setState({ contacts: res.contact_list, isLoadingNumbers: false })
          } else {
            this.setState({ isLoadingNumbers: false })
            Alert.alert('Error', `${res.error}! try again.`);
          }
        } catch (error) {
          console.log('error: ', error);
          this.setState({ isLoadingNumbers: false })
          Alert.alert('Error', 'request failed! try again.')
        }
      }
    }
  
    getContactName(number) {
      const { phoneContacts, countryCode } = this.state;
      if (phoneContacts.length) {
        let index = phoneContacts.findIndex(element =&gt; {
          let parsedNumber = element.phoneNumbers[0] ? element.phoneNumbers[0].number.replace(/[^a-zA-Z0-9]/g, '') : "";
          if (parsedNumber) {
            if (parsedNumber.slice(0, 1) === "0") {
              if (parsedNumber.slice(1, 2) === "0") {
                if (parsedNumber.substring(2) === number ||
                  parsedNumber.substring(2).replace(`${countryCode}0`, countryCode) === number) {
                  return number;
                }
              } else {
                return countryCode + parsedNumber.substring(1) === number;
              }
            } else {
              // console.log('nummm: ', parsedNumber, number);
              // return parsedNumber === number;
              if (parsedNumber === number ||
                parsedNumber.replace(`${countryCode}0`, countryCode) === number) {
                return number;
              }
            }
          }
  
        });
        if (Platform.OS === 'ios') {
          return index &gt;= 0 ? phoneContacts[index].givenName : ''
        } else {
          return index &gt;= 0 ? phoneContacts[index].displayName : ''
        }
      }
    }
  </code></pre>
  
  <p>The below code is where I render the name and number on the phone in a list. </p>
  
  <pre><code>            &lt;View style={styles.container}&gt;
                {this.state.contacts.length ?
                  &lt;FlatList
                    data={this.state.contacts}
                    extraData={this.state}
                    numColumns={1}
                    keyExtractor={(item, index) =&gt; index.toString()}
                    renderItem={({ item }) =&gt; (
                      &lt;View style={styles.itemContainer}&gt;
                        &lt;View&gt;
                          &lt;Text style={{ color: '#000', fontSize: 20 }}&gt;
                            {this.getContactName(item.phone_number)}&lt;/Text&gt;
                          &lt;Text style={{ color: '#000' }}&gt;
                            {item.phone_number}&lt;/Text&gt;
                        &lt;/View&gt;
                        {item.selected ?
                          &lt;TouchableOpacity onPress={() =&gt; this.removeContact(item)}&gt;
                            &lt;Icon name={"ios-add-circle"} size={26} color={primaryColor} /&gt;
                          &lt;/TouchableOpacity&gt;
                          :
                          &lt;TouchableOpacity onPress={() =&gt; this.selectContact(item)}&gt;
                            &lt;Icon name={"ios-add-circle"} size={26} color={'grey'} /&gt;
                          &lt;/TouchableOpacity&gt;
                        }
                      &lt;/View&gt;
                    )}
                  /&gt;
  </code></pre>
  
  <p>Hopefully it is clear what I have done so far. What I need now I think anyway. Is another function to pull the name and number together and sort them before I render them. At least thats what I think. Maybe the sorting needs to be done in the render. I'm not 100%. Ive been trying all sorts of ways for two days now and just can't seem to get it to sort the contacts list alphabetically. Any and all help would be so greatly appreciated.</p>