问题
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