如何根据Dart中的int属性对所有Class属性进行排序?

时间:2017-12-17 02:13:44

标签: list class sorting dart flutter

我有Firebase信息我回来了包含一个int属性值。我已成功将它们存储为一个类。现在我想将该类设置为Map或List,然后根据我的int值对它们进行降序排序,并创建每个属性的列表以便稍后填充元素。这是我的代码......

class Top {
  String videoId;
  int rank;
  String title;
  String imageString;

  Top({this.videoId, this.rank, this.title, this.imageString});
}

List<Top> videos = new List();
List onesRank = new List();
List onesIds = new List();

for (var items in titles) {

var top = new Top(videoId: items['vidId'], rank: items['Value'],
title: items['vidTitle'], imageString: items['vidImage']);

onesRank.add(top.rank);
onesIds.add(top.videoId);
                      }

print(onesRank);
print(onesIds);

打印print(top.rank)成功记录我的int值,并print(onesIds)正确记录我的ID ...

[10, 6, 14, 12, 11, 5, 10, 1] [4408NthSJis, 7n5ieHnu90w, XuSYtAsMxfY, 9bZkp7q19f0, sGRv8ZBLuW0, 2ips2mM7Zqw, 0Pinupmqwaw, m8MfJg68oCs]

但我想按降序排列元素顺序排列所有top属性为top.rank,如此...

[14, 12, 11, 10, 10, 6, 5, 1] [XuSYtAsMxfY,9bZkp7q19f0,sGRv8ZBLuW0,4408NthSJis,0Pinupmqwaw,7n5ieHnu90w, 2ips2mM7Zqw,m8MfJg68oCs]

我在Swift中完成了这个......

for items in snapshot.value as! NSDictionary
                {
                    let ids = items.value["vidId"] as! String
                    let value = items.value["Value"] as! Int
                    let pics = items.value["vidImage"] as! NSString
                    let title = items.value["vidTitle"] as! String

                    let top = Top(videoID: ids, value: value, title: title, imageString: pics as String)
                    videos.append(top)
                }
                videos.sort { $0.value > $1.value }

                self.topTitle = videos.map { $0.title }
                self.topID = videos.map { $0.videoID }
                self.topImage = videos.map { $0.imageString }
                self.vidRank = videos.map { $0.value }

2 个答案:

答案 0 :(得分:0)

我仍然不明白你想要完成什么。

如果您有if currentQuestion != questions.count { } 的清单,则可以

Top

答案 1 :(得分:0)

根据您的Swift代码:

import 'package:queries/collections.dart';

void main() {
  var videos = Collection(geVideos())
      .select((e) => Top(
          videoId: e['vidId'],
          rank: e['Value'],
          title: e['vidTitle'],
          imageString: e['vidImage']))
      .orderByDescending((e) => e.rank);

  var topRank = videos.select((e) => e.rank);
  var topID = videos.select((e) => e.videoId);
  print(topRank.toList());
  print(topID.toList());
}

List<Map> geVideos() {
  var ranks = Collection([10, 6, 14, 12, 11, 5, 10, 1]);
  var ids = Collection([
    '4408NthSJis',
    '7n5ieHnu90w',
    'XuSYtAsMxfY',
    '9bZkp7q19f0',
    'sGRv8ZBLuW0',
    '2ips2mM7Zqw',
    '0Pinupmqwaw',
    'm8MfJg68oCs'
  ]);
  var q = ranks.zip(ids, (x, y) => {'vidId': y, 'Value': x});
  return q.toList();
}

class Top {
  String videoId;
  int rank;
  String title;
  String imageString;
  Top({this.videoId, this.rank, this.title, this.imageString});
}

结果:

[14, 12, 11, 10, 10, 6, 5, 1] [XuSYtAsMxfY, 9bZkp7q19f0, sGRv8ZBLuW0, 4408NthSJis, 0Pinupmqwaw, 7n5ieHnu90w, 2ips2mM7Zqw, m8MfJg68oCs]