React Native - FlatList ordering without sorting data

早过忘川 提交于 2019-12-02 12:25:47

问题


Is there a way to render Flatlist rows in a certain order without sorting the data source?

Example

<FlatList
  data={[{key: 'a'}, {key: 'c'}, {key: 'b'}]}
  renderItem={({item}) => <Text>{item.key}</Text>}
/>

The above code renders a list

a
c
b

Is there a way to get it to render the following without sorting the data source?

a
b
c

回答1:


Basically you are saying, I want Pizzar, but do not use sausage, meat, whatever. The answer is therefore No.

In order to perform efficient sorting and ordering, you can try linq.js

linq.js, in short is LINQ for JavaScript. Some particular useful/pertinent features include:

  • implement all .NET 4.0 methods and many extra methods (inspiration from Rx, Achiral, Haskell, Ruby, etc...)
  • complete lazy evaluation
  • binding for Reactive Extensions for JavaScript(RxJS) and IntelliSense

If Linq.JS can solve your performance issue, you should sort it first and then render the sorted data source.

Another option is to use lodash, whose performance is claimed to be even better, see this link for details.

Here are the code snippets to sort some data: LINQ.JS:

var queryResult = $.Enumerable.From(jsonArray)
  .OrderBy(function(x) {
    return x.user.screen_name
  })
  .Select(function(x) {
    return x.user.screen_name + ':' + x.text
  })
  .ToArray();

Lodash:

var queryResults = _.chain(jsonArray)
.sortBy(function (x) { return x.user.screen_name })
.map(function(x) {
    return x.user.screen_name + ':' + x.text
  })
.value();


来源:https://stackoverflow.com/questions/45229682/react-native-flatlist-ordering-without-sorting-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!