javascriptreactjsreact-nativereact-android

sorting the items in the flatList


In my react native application, I am showing services that are offered by my office and the location of those services in a flatlist. I am also showing the miles of the service from the persons current address in that same flatlist. I want those services to be sorted by miles. Below is what my screen data looks like:

Services are available in the following locations:

123 Test Drive
(313) 231-4560
Miles: 7
__________________________

234 Test Drive
(450) 222-9999
Miles: 3
____________________________

121 Addr Drive
(560) 455-1211
Miles: 1

Is their any way I can display these miles by sorting them from least to the highest so I want to display the service that has 1 Miles at the top and then 3 miles after it and then 7 miles after that. Below is the sample:

121 Addr Drive
(560) 455-1211
Miles: 1
____________________________

234 Test Drive
(450) 222-9999
Miles: 3
____________________________

123 Test Drive
(313) 231-4560
Miles: 7

the only issue is that I am calculating these miles. The miles are not coming from my JSON file directly. I have Longitude and Latitude in my JSON file and I am getting the miles by using geodist to get the exact miles.

var geodist = require("geodist");
var dist = geodist(
  { lat: item.cLat, lon: item.cLong },
  { lat: item.LatL, lon: item.Long2 },
  "mi"
);

Below is my code for displaying the services address, Miles.

render() {
  return (
    <View>
      <View>
        <Text style={styles.title}>
          {this.props.navigation.state.params.item.ser}
        </Text>
        <Text style={styles.SerContent}>
          Service is available in the following locations:
        </Text>
      </View>

      <View>
        <FlatList
          data={newList}
          ItemSeparatorComponent={this.FlatListItemSeparator}
          renderItem={this._renderItem}
          keyExtractor={(item, index) => index}
        />
      </View>
    </View>
  );
}

Below is the code for {_renderItem}

_renderItem = ({ item }) => {
  var geodist = require("geodist");

  var dist = geodist(
    { lat: item.cLat, lon: item.cLong },
    { lat: item.LatL, lon: item.Long2 },
    "mi"
  );

  return (
    <View>
      <Text style={styles.Address1}>{item.addr} </Text>
      <Text style={styles.Address1}>{item.phone}</Text>
      <Text style={styles.AddressSpace}>Miles:{dist}</Text>
    </View>
  );
}

Below is my JSON file:

[
  {
    id: "1",
    fk: 1,
    addr: "123 test drive",
    phone: "(313) 231-4560",
    LatL: "33.9387",
    Long2: "-117.284",
    cLat: "33.931",
    cLong: "-117.40"
  },

  {
    id: "2",
    fk: 1,
    addr: "234 Test Drive",
    phone: "(450) 222-9999",
    LatL: "33.977",
    Long2: "-117.37",
    cLat: "33.93",
    cLong: "-117.409"
  },

  {
    id: "3",
    fk: 1,
    addr: "121 Addr drive",
    phone: "560) 455-1211",
    LatL: "33.76",
    Long2: "-116.97",
    cLat: "33.9319",
    cLong: "-117.409"
  }
];

Any help will be greatly appreciated.


Solution

  • You could sort the array by distance before you give it as data to the FlatList

    It would probably be a good idea to put the distance in the state, so that you don't have to do the calculations in both the sorting and in the _renderItem method.

    <FlatList
      data={newList.sort((a, b) => {
        const aDist = geodist(
          { lat: a.cLat, lon: a.cLong },
          { lat: a.LatL, lon: a.Long2 },
          "mi"
        );
        const bDist = geodist(
          { lat: b.cLat, lon: b.cLong },
          { lat: b.LatL, lon: b.Long2 },
          "mi"
        );
    
        return aDist - bDist;
      })}
      ItemSeparatorComponent={this.FlatListItemSeparator}
      renderItem={this._renderItem}
      keyExtractor={(item, index) => index}
    />