user2540748
user2540748

Reputation:

React native render a listview by order

I am rendering a list view using:

  render() {
    return (
      <ListView
        dataSource={this.state.dataSource}
        renderRow={this.renderRow.bind(this)}/>
    );
  }

and

var dataSource = new ListView.DataSource(
  {rowHasChanged: (r1, r2) => r1.id !== r2.id});
this.state = {
  dataSource: dataSource.cloneWithRows(this.props.deals)
};

I'm hoping to be able to sort this by a value on the datasource (this.props.deals.distance). A look online and through the docs I can't seem to find a way to do it. Any advice?

Upvotes: 4

Views: 4083

Answers (1)

agenthunt
agenthunt

Reputation: 8678

Just sort the deals array and set the dataSource.

let sortedDeals = this.props.deals.sort((a,b) => {
if (a.distance < b.distance) {
    return -1;
  }
  if (a.distance > b.distance) {
    return 1;
  }
  // a must be equal to b
  return 0;
});
let dataSource = dataSource.cloneWithRows(sortedDeals) 
this.setState({
    dataSource: dataSource
});

Upvotes: 6

Related Questions