关于排序:如何根据函数返回的值对dart中的List进行排序

How to sort List in dart based on value returned by a function

我必须按最近的位置显示商店列表
我有一个未排序的商店列表和一个要计算的函数
每个商店到当前位置的距离,但我不知道如何
根据函数返回的值对dart中的List进行排序。
我从api获取未排序的商店数据列表。
对于这个问题,我需要逻辑来对_kikkleStores List

进行排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  class KikkleStoresBloc extends BlocBase {
  List<KikkleStoreInfo> _kikkleStores = [];
  //for distance sorting
  List<KikkleStoreInfo> _kikkleStoresSorted = [];
  List<double> distanceFromCurrentLocation;//already got
  value in it
  bool hasReachedEndOfList = false;
  Coordinate _currentLocation;

  KikkleStoresBloc();

  final _kikkleStoreSubject =
  BehaviorSubject<List<KikkleStoreInfo>>
  ();

  // Getter Stream
  Stream<List<KikkleStoreInfo>> get kikkleStores =>
  _kikkleStoreSubject.stream;`enter code here`

  // Getter Sink
  Function(List<KikkleStoreInfo>) get _fetchedkikkleStores =>
  _kikkleStoreSubject.sink.add;

  getKikkleStores() async {
    try {
      if (_currentPage <= _totalPages) {
        final response = await
         ApiController.getKikkleStores(_currentPage);
        _kikkleStores.addAll(response.item1);



       //here how to sort _kikkleStores by using
       getStoreDistance function



        _totalPages = response.item3;
        _fetchedkikkleStores(_kikkleStores);
        if (_currentPage == _totalPages) {
          hasReachedEndOfList = true;
        } else if (_currentPage < _totalPages &&
        _kikkleStores.length
        < 10) {

              }
            }
          }
         }

  // this function returns distance
  getStoreDistance(Coordinate currentLocation, KikkleStoreInfo
  store)
  async {
    if (currentLocation == null) return 0.0;
    try {
      double distanceInMeter = await
      LocationUtils.getDistanceInMeters(
      currentLocation, Coordinate(store.latitude,
      store.longitude));
      // final miles = (distanceInMeter / 1609.344).round();
      return distanceInMeter;
    } catch (e) {
      return 0.0;
    }
  }
  void getCurrentLocation() async {
    try {
      final isAllowed = await
      PermissionsUtils.isLocationAccessAllowed();
      if (isAllowed) {
        final coordinates = await
        LocationUtils.getCurrentLocation();
        if (coordinates != null) {
          _currentLocation = coordinates;
        }
      }
     }
     catch (e) {
      print(e);
    }
  }
}

参考以下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void main(){
  List<POJO> pojo = [POJO(5), POJO(3),POJO(7),POJO(1)];
// fill list

pojo..sort((a, b) => a.id.compareTo(b.id));
  for(var i in pojo){
    print(i.id);      // prints list in sorted order i.e 1 3 5 7
  }

}

class POJO {
  int id;

  POJO(this.id);
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
 void sortfun() async {
    for (int c = 0; c < (_kikkleStores.length - 1); c++) {
      for (int d = 0; d < _kikkleStores.length - c - 1; d++) {
        if (await getStoreDistance(_currentLocation, _kikkleStores[d]) >
            await getStoreDistance(_currentLocation,
                _kikkleStores[d + 1])) /* For descending order use < */
        {
          swap = _kikkleStores[d];
          _kikkleStores[d] = _kikkleStores[d + 1];
          _kikkleStores[d + 1] = swap;
        }
      }
    }
  }